Wpf UWP XAML如何在绑定的ListView中包装文本
如何在listview控件中换行或以其他方式显示长字符串。在绑定的ListView控件中包装或以其他方式显示长文本时失败。我的xaml页面基本上是一个绑定FlipView,带有一个ItemTemplate,其中包含两个绑定TextBlock和一个绑定ListView。我可以包装文本块,但不能包装listviewitems。这似乎是一件很简单的事情,但我却无法理解 以下是我的xaml的一部分:Wpf UWP XAML如何在绑定的ListView中包装文本,wpf,xaml,listview,Wpf,Xaml,Listview,如何在listview控件中换行或以其他方式显示长字符串。在绑定的ListView控件中包装或以其他方式显示长文本时失败。我的xaml页面基本上是一个绑定FlipView,带有一个ItemTemplate,其中包含两个绑定TextBlock和一个绑定ListView。我可以包装文本块,但不能包装listviewitems。这似乎是一件很简单的事情,但我却无法理解 以下是我的xaml的一部分: <Page.Resources> <DataTemplate x:DataTy
<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 ;
}
}
//c#
公共类mydataObject
{
公共int Id{get;set;}
公共数据对象对象{get;set;}
公开列出对象详细信息{get;set;}
公共重写字符串ToString()
{
返回this.theObject.Subject;
}
}
公共类dataObjectDetails
{
公共int Id{get;set;}
公共整数dodId{get;set;}
公共字符串bodyText{get;set;}
公共重写字符串ToString()
{
返回正文;
}
}
为ListView提供一个ItemTemplate,它将内容放在包装文本的文本块中:
<ListView
ItemsSource="{x:Bind theObjectDetails, Mode=OneWay}"
HorizontalAlignment="Stretch"
>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock
Text="{Binding bodyText}"
TextWrapping="WrapWholeWords"
/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我应该指出Windows 10 UWP ListView,因为它可能会有所不同……您传递到ListView中的项目是否只有文本字符串?或者你在里面扔东西,比如巴顿什么的?如果只是字符串,您可以始终使用绑定到内容的文本块指定listview ItemTemplate并实现目标。我开始理解我的困难。我将ListView绑定到一个对象,该对象在ToString上有一个覆盖,用于发送ListView中显示的文本。因此,FlipView被绑定到一个列表,ListView被绑定到您刚才添加的代码,您将ListView.ItemsSource绑定到类名dataObjectDetails
,而不是属性theObjectDetails
。您是否在ListView中看到了预期的项目?我已更正了上述示例。看来我在这里发布泛型的时候太粗心了。这个答案就是解决办法。绑定一开始让我很不舒服,因为Text属性需要绑定到底层dataObjectDetails BodyText,或者仅仅绑定到BodyText。