Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP XAML How do I wrap text in a bound ListView

Tags:

listview

wpf

xaml

How do I wrap or otherwise display long strings in my listview control. I have been unsuccessful in wrapping, or otherwise displaying long text in my bound ListView control. My xaml page is basically a BOUND FlipView with an ItemTemplate that contains two bound textBlocks and a bound ListView. I can get the TextBlocks to wrap but not the listviewitems. It would seem like such a simple thing yet it eludes me.

Here is a portion of my xaml:

<Page.Resources>
    <DataTemplate x:DataType="data:MydataObject" x:Key="MydataObjectTemplate">
        <StackPanel HorizontalAlignment="Stretch" Height="596" Width="982">
            <TextBlock Name="txtDataObjectId" Text="{Binding dataObject.Id}" Visibility="Collapsed" TextWrapping="WrapWholeWords"/>
            <TextBlock FontSize="24" Text="{x:Bind dataObject}" HorizontalAlignment="Center" TextWrapping="WrapWholeWords"/>
            <ListView ItemsSource ="{x:Bind theObjectDetails, Mode=OneWay }" 
                      HorizontalAlignment="Stretch"  
                      BorderBrush="Black" 
                      BorderThickness="1"/>
        </StackPanel>
    </DataTemplate>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel HorizontalAlignment="Stretch">
        <ComboBox x:Name="cboCategory" Header="Category" SelectionChanged="cboCategory_SelectionChanged" />
        <FlipView x:Name="FlipView1" 
              ItemsSource="{x:Bind MydataObjects, Mode=OneWay }" 
              ItemTemplate="{StaticResource MydataObjectTemplate}"
              BorderBrush="Black" 
              BorderThickness="1"/>
    </StackPanel>
</Grid>

//c#
public class mydataObject
{
    public int Id { get; set; }
    public dataObject theObject { get; set; }
    public List<dataObjectDetails> theObjectDetails { get; set; }

    public override string ToString()
    {
        return this.theObject.Subject;
    }
}

public class dataObjectDetails
{
    public int Id { get; set; }
    public int dodId{ get; set; }
    public string bodyText { get; set; }

    public override string ToString()
    {
        return bodyText ;
    }
}
like image 496
Mark Johnson Avatar asked Feb 26 '26 13:02

Mark Johnson


1 Answers

Give the ListView an ItemTemplate, which puts the content in a TextBlock that wraps the text:

<ListView
    ItemsSource="{x:Bind theObjectDetails, Mode=OneWay}"
    HorizontalAlignment="Stretch"
    >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock 
                    Text="{Binding bodyText}"
                    TextWrapping="WrapWholeWords"
                    />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
like image 151
15ee8f99-57ff-4f92-890c-b56153 Avatar answered Feb 28 '26 06:02

15ee8f99-57ff-4f92-890c-b56153



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!