Search code examples
windows-phone-7xamlwindows-phone-8textblock

In windows phone develop,can I set a width to textblock,and then display like this?


For the purpose that I want to make the item in the list box looks nice

<ListBox
   Height="Auto"
   HorizontalAlignment="Left" 
   Margin="0,0,0,0" 
   Name="hotSpotListBox" 
   VerticalAlignment="Top"
   Width="460" 
   ItemsSource="{Binding}" 
   ScrollViewer.VerticalScrollBarVisibility="Disabled"  
   SelectionChanged="hotSpotListBoxSelectionChanged" >

<ListBox.ItemTemplate>
  <DataTemplate>
     StackPanel  Margin="10,0,12,4 
                 Orientation="Vertical">

<StackPanel Orientation="Horizontal">
 <TextBlock 
      Width="380" 
      Margin="0,4,12,4"
      FontSize="24" 
      Text="{Binding hotname}" 
      Foreground="#FF407CED"  
      TextWrapping="Wrap" 
      Tap="QueryHotSpotItem_Tap"/>

 <Image 
    Margin="0,0,0,0" 
    Source="/Images/ico_name.png" 
    Tap="HotSpotIcon_Tap"/>
       </StackPanel>


  <TextBlock
    Margin="0,0,12,4" 
    FontSize="22"
    Text="{Binding address}" 
    Foreground="Black" 
    TextWrapping="Wrap" 
    Tap="QueryHotSpotItem_Tap"/>


<TextBlock 
  Margin="0,4,12,4"
  FontSize="24" 
  Text="{Binding city}"  
  Height="1"
  Width="1" 
  Visibility="Collapsed"/>



<TextBlock 
  Margin="0,0,12,4" 
  FontSize="22" 
  Text="{Binding hottype}" 
  Height="1" 
  Width="1" 
  Visibility="Collapsed" />



<TextBlock 
 Margin="0,0,12,4" 
 FontSize="2"
 Text="{Binding coverarea}"
 Height="1" 
 Width="1" 
 Visibility="Collapsed" />



<TextBlock 
   Margin="0,0,12,4" 
   FontSize="2" 
   Text="{Binding longitude}" 
   Height="1" 
   Width="1"
   Visibility="Collapsed" />

<TextBlock
   Margin="0,0,12,4" 
   FontSize="2" 
   Text="{Binding latitude}" 
   Height="1"
   Width="1" 
   Visibility="Collapsed" />
             </StackPanel>
           </DataTemplate>
         /ListBox.ItemTemplate>
       </ListBox>

Please look at the comments in the code:
great programmers !!!watch out!!
here,width=380 is for a nice layout,and textwrapping=“wrap” is to prevent
some hotname is too long
but now I want to keep the width=380
but if the hotname is too long to display,it display"longhotname..."
how can i implement this ???

Any advice would be nice,Thank you!!!


Solution

  • use TextTrimming property.

    TextBlock text = new TextBlock();
    text.TextTrimming = TextTrimming.WordEllipsis;