2011-11-30 32 views
4

我有一個使用數據模板的列表框,模板中的一個元素是一個文本塊。問題在於單詞不會換行,我不想設置固定大小。任何知道如何解決這個問題的人?這讓我瘋狂!如何讓我的文字包裝? wpf問題

    <ListBox Grid.Row=" 1" HorizontalContentAlignment="Stretch" Background="#24221f" ItemsSource="{Binding Messages}" ScrollViewer.VerticalScrollBarVisibility="Visible" ClipToBounds="False" BorderBrush="{x:Null}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate > 
         <Border BorderBrush="#24221f" BorderThickness="3" Width=" auto"> 
          <DockPanel Background="{StaticResource blackBackground}" HorizontalAlignment="Stretch" Width="auto"> 
           <Border BorderThickness="3" BorderBrush="Transparent"> 
          <Image Source="{Binding IconImageUrl}" VerticalAlignment="top" Height="22" Width ="22" DockPanel.Dock="Left" /> 
           </Border> 
           <Border BorderThickness="3" BorderBrush="LightGray" Height="auto" Width="auto" HorizontalAlignment="Left" VerticalAlignment="Center" DockPanel.Dock="Left"> 
          <Image Source="{Binding ProfileImageUrl}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="48" Width ="48" /> 
           </Border> 
           <StackPanel Orientation="Vertical" DockPanel.Dock="Left" Margin="5,0,0,0"> 
            <Label Content="{Binding Path=Sender}" Foreground="#feb41c" FontFamily="Verdana" FontWeight="Bold" FontSize="14" /> 
            <TextBlock Width="100" Text="{Binding Path=ShortMessage}" Margin="10,0,0,0" Foreground="BlanchedAlmond" TextWrapping="Wrap" FontFamily="Verdana" /> 
            <Label Content="{Binding Path=Time}" Margin="10,0,0,0" Foreground="DarkGray" FontFamily="Verdana" FontStyle="Italic"/> 
          </StackPanel> 
         </DockPanel> 
         </Border> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

回答

2

只是ListBoxItems的Horizo​​ntalContentAlignment屬性設置爲「拉伸」嘗試使用樣式

<Style TargetType="{x:Type ListBoxItem}" > 
     <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
</Style> 

,並禁用Horizo​​ntalScrollBar visibilit ÿ

ScrollViewer.HorizontalScrollBarVisibility="Disabled" 

更新

<Window.Resources> 
    <SolidColorBrush x:Key="blackBackground" Color="Black"/> 
    <Style TargetType="{x:Type ListBoxItem}" > 
     <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    </Style> 

</Window.Resources> 
<Grid> 
    <ListBox Grid.Row=" 1" HorizontalContentAlignment="Stretch" Background="#24221f" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
      ItemsSource="{Binding Messages}" ScrollViewer.VerticalScrollBarVisibility="Visible" ClipToBounds="False" BorderBrush="{x:Null}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate > 
       <Border BorderBrush="#24221f" BorderThickness="3" Width=" auto"> 
        <DockPanel Background="{StaticResource blackBackground}" HorizontalAlignment="Stretch" Width="auto"> 
         <Border BorderThickness="3" BorderBrush="Transparent"> 
          <Image Source="{Binding IconImageUrl}" VerticalAlignment="top" Height="22" Width ="22" DockPanel.Dock="Left" /> 
         </Border> 
         <Border BorderThickness="3" BorderBrush="LightGray" Height="auto" Width="auto" HorizontalAlignment="Left" VerticalAlignment="Center" DockPanel.Dock="Left"> 
          <Image Source="{Binding ProfileImageUrl}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="48" Width ="48" /> 
         </Border> 
         <StackPanel Orientation="Vertical" DockPanel.Dock="Left" Margin="5,0,0,0"> 
          <Label Content="{Binding Path=Sender}" Foreground="#feb41c" FontFamily="Verdana" FontWeight="Bold" FontSize="14" /> 
          <TextBlock Text="{Binding Path=ShortMessage}" Margin="10,0,0,0" Foreground="BlanchedAlmond" TextWrapping="Wrap" FontFamily="Verdana" /> 
          <Label Content="{Binding Path=Time}" Margin="10,0,0,0" Foreground="DarkGray" FontFamily="Verdana" FontStyle="Italic"/> 
         </StackPanel> 
        </DockPanel> 
       </Border> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</Grid> 
+1

謝謝你的回答,但它沒有工作.. –

+0

我已經更新了答案樣本我做了..請看看這個 –

+1

非常感謝你@Kishore庫馬爾,這工作!發送給你一個來自瑞典的虛擬金色星星:) –

0

我覺得這個線程的答案是你的問題,請參見accespted答案由「納什」 - Force TextBlock to wrap in WPF ListBox

(記得要給予好評的答案鏈接的線程,如果它可以幫助你:))

+0

嗨,謝謝你的回答,但我已經試過了,並沒有工作... –

4

StackPanel的是惡:=)當我有在XAML怪異的行爲,其中包括的StackPanel,切換到右參數的網格(固定大小的,或分或「自動」)經常解決這個問題。 請注意,由於您設置了第一張圖像(IconImageUrl)的DockPanel.Dock,因此它在xaml中存在錯誤,而在它的邊框中,您應該設置它。這可能會讓佈局執行奇怪的事情。

+0

+1,'StackPanel'不好的原因是它總是給它的孩子提供他們要求的任何高度/寬度。在這種情況下,它認爲'TextBlocks'需要顯示全文的空間,所以包裝不會發生。 –