How can I wrap custom text?

I have a list that uses datatemplates, and one of the elements in the template is a text block. The problem is that the words will not be wrapped, and I do not want to set a fixed size. Who knows how to solve this problem? It drives me crazy!

<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> 
+4
source share
3 answers

just try using the HorizontalContentAlignment property to "stretch" ListBoxItems using the style

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

and also turn off the visibility of the HorizontalScrollBar

 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 

Update

  <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> 
+2
source

StackPanel Evil: =) when I have strange behavior in xaml that includes StackPanel, switching to the grid with the correct parameters (fixed size or stars or โ€œautoโ€) often fixes the problem. Please also note that there is an error in your xaml because you set the DockPanel.Dock of your first image (IconImageUrl), while it is on the border that surrounds it, which you must set. This can make the layout do strange things.

+4
source

I think this thread will answer your question, see the introductory answer from "Nash" - Force TextBlock for transferring to WPF ListBox

(and remember, in order to support the response, a related thread if it helps you :))

0
source

Source: https://habr.com/ru/post/1383905/


All Articles