UWP XAML如何在绑定的ListView中包装文本

时间:2016-01-07 19:11:55

标签: wpf xaml listview

如何在listview控件中包装或以其他方式显示长字符串。我在包装或以其他方式在绑定的ListView控件中显示长文本时未成功。我的xaml页面基本上是一个带有ItemTemplate的BOUND FlipView,它包含两个绑定的textBlocks和一个绑定的ListView。我可以让TextBlocks包装而不是listviewitems。这似乎是一件很简单的事情,但却让我望而却步。

以下是我的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 ;
    }
}

1 个答案:

答案 0 :(得分:1)

为ListView提供一个ItemTemplate,它将内容放在一个包装文本的TextBlock中:

<ListView
    ItemsSource="{x:Bind theObjectDetails, Mode=OneWay}"
    HorizontalAlignment="Stretch"
    >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock 
                    Text="{Binding bodyText}"
                    TextWrapping="WrapWholeWords"
                    />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>