2010-10-20 19 views
2

我是WPF的新手,我需要你的幫助 - 請。 :)Wpf - 不能使文本換行

我需要一個ItemsControl只有垂直滾動,如果項目不適合他們必須包裝。

我做了我的代碼一個很小的例子:

<Grid>  
    <ItemsControl Margin="64,73,65,76" BorderThickness="1" Name="lst" HorizontalContentAlignment="Stretch" Background="White" BorderBrush="#FFBABABA"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate DataType="{x:Type local:Song}"> 
      <Border x:Name="personsBorder" CornerRadius="3" Background="#FFD8ECFC" Margin="1,1,1,1" Padding="2,2,2,2"> 
       <StackPanel Orientation="Horizontal"> 
        <Image Width="16" Height="16" Source="icon.png" /> 
        <TextBlock x:Name="txtLyric" Text="{Binding Lyric}" Padding="2,2" Foreground="Black" Height="Auto" TextTrimming="WordEllipsis" TextWrapping="WrapWithOverflow" /> 
       </StackPanel> 
      </Border> 
     </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.Template> 
      <ControlTemplate TargetType="{x:Type ItemsControl}"> 
       <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"> 
        <ItemsPresenter></ItemsPresenter> 
       </ScrollViewer> 
      </ControlTemplate> 
     </ItemsControl.Template> 
    </ItemsControl> 
</Grid> 

Public Class Song 
    Public Property Lyric As String 
    Public Sub New(ByVal lyric As String) 
     Me.Lyric = lyric 
    End Sub 
End Class 


Class MainWindow 
    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded 
     Dim l As New List(Of Song) 
     l.Add(New Song("This is first line")) 
     l.Add(New Song("The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog")) 
     l.Add(New Song("This is third line")) 
     Me.lst.ItemsSource = l 
    End Sub 
End Class 

任何想法,爲什麼我的商品沒有包裝?

非常感謝!

回答

1

只是試試這個

<Grid> 
     <ItemsControl Margin="64,73,65,76" BorderThickness="1" Name="lst" HorizontalContentAlignment="Stretch" Background="White" BorderBrush="#FFBABABA"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate DataType="{x:Type local:Song}"> 
        <Border x:Name="personsBorder" CornerRadius="3" Background="#FFD8ECFC" Margin="1,1,1,1" Padding="2,2,2,2"> 
         <Grid>   
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="Auto"></ColumnDefinition> 
           <ColumnDefinition></ColumnDefinition> 
          </Grid.ColumnDefinitions> 
          <!--<Image Width="16" Height="16" Source="icon.png" />--> 
          <Rectangle Fill="Red" Height="16" Width="16"></Rectangle> 
          <TextBlock Grid.Column="1" x:Name="txtLyric" Text="{Binding Lyric}" Padding="2,2" Foreground="Black" Height="Auto" TextTrimming="WordEllipsis" TextWrapping="WrapWithOverflow" /> 
         </Grid> 
        </Border> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
      <ItemsControl.Template> 
       <ControlTemplate TargetType="{x:Type ItemsControl}"> 
        <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"> 
         <ItemsPresenter></ItemsPresenter> 
        </ScrollViewer> 
       </ControlTemplate> 
      </ItemsControl.Template> 
     </ItemsControl> 
    </Grid> 
+0

謝謝!並感謝您的代碼! – MojoDK 2010-10-21 05:31:41

1

的文字塊的內容可以使用屬性「TextWrapping」像你這樣做包裹。不要使用Stackpanel,請嘗試使用Dockpanel /網格。

+0

謝謝你幫我出去! – MojoDK 2010-10-21 05:31:57