pay
pay

Reputation: 386

Is there a simple way to wrap text in a WPF ListBox?

I have searched around for this, found some solutions related to Winforms, and some even just saying it is really difficult in WPF to accomplish, but those posts are quite old.

If I have a standard ListBox, which is declared as:

<ListBox 
    x:Name="listBox" 
    HorizontalAlignment="Left" 
    Height="240" 
    Margin="401,68,0,0" 
    VerticalAlignment="Top" 
    Width="345" 
    SelectionChanged="listBox_SelectionChanged"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    Grid.ColumnSpan="2"/>`

and programmatically:

System.ComponentModel.BindingList<string> listItems = new System.ComponentModel.BindingList<string>();
listBox.ItemsSource = listItems;

Is there a way for these strings to be wrapped within the ListBox?

Upvotes: 3

Views: 7445

Answers (2)

Ranch Camal
Ranch Camal

Reputation: 570

If you got StackPanel please get rid of them. They are bad with wrapping. Use GRID instead like @EdPlunkett suggested.

To use this:

<ItemsControl ItemsSource="{Binding MyErrors, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                          Template="{StaticResource ErrorListContainerTemplate}"
                                          ItemContainerStyle="{StaticResource ErrorListStyle}"
                                         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                          />

Here is my style code:

<Style TargetType="{x:Type TextBlock}" x:Key="WrappingStyle">
    <Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
<Style TargetType="ContentPresenter" x:Key="ErrorListStyle">
    <Setter Property="TextBlock.Foreground" Value="{DynamicResource TextBoxBorderErrorColor}"/>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border Margin="0,5">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Path Grid.Column="0" Fill="{DynamicResource TextBoxBorderErrorColor}" VerticalAlignment="Top" HorizontalAlignment="Left">
                            <Path.Data>
                                <EllipseGeometry RadiusX="2.5" RadiusY="2.5"/>
                            </Path.Data>
                        </Path>
                        <ContentPresenter Grid.Column="1" Content="{Binding}" VerticalAlignment="Top">
                            <ContentPresenter.Resources>
                                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource WrappingStyle}"/>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                        </Grid>
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 0

Not hard at all:

<ListBox
    ....
    >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock
                    Text="{Binding}"
                    TextWrapping="Wrap"
                    />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 8

Related Questions