[英]WPF ListView->GridView->StackPanel
I have a ListView that uses GridView to display some data. 我有一个使用GridView显示一些数据的ListView。 One of the fields contains a list with image paths. 其中一个字段包含带有图像路径的列表。 Therefore I defin a GridViewColumn and create a DataTemplate. 因此,我定义了一个GridViewColumn并创建了一个DataTemplate。 Dis DataTemplate contains another ListView with the Images list as new DataContext. Dis DataTemplate包含另一个ListView,其中Images列表作为新的DataContext。 When I don't write anything in the inner StackPanel, I can see that a list of string (in the format AppName.ClassName) is displayed in left to right order. 当我不在内部StackPanel中写任何东西时,我可以看到字符串列表(格式为AppName.ClassName)以从左到右的顺序显示。 But whenever I try to display the strings as something else, eg 但每当我尝试将字符串显示为其他内容时,例如
<Image Source="{Binding Name}" Height="32"/>
I get an System.Windows.Markup.XamlParseException . 我得到一个System.Windows.Markup.XamlParseException。 Even with data binding I get an exception. 即使有数据绑定,我也会遇到异常。 Eg 例如
<Image Source="Images/Camera_32xLG.png" Height="32"/>
. 。 Any hint, what I might do wrong? 任何提示,我可能做错了什么?
<GridViewColumn Header="Images">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Images}" BorderThickness="0">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
--> here is where I don't know what to do next
</StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Since the StackPanel
is a part of ItemsPanelTemplate
, you should just define it's properties and not explicitly add any children. 由于StackPanel
是ItemsPanelTemplate
的一部分,因此您应该只定义它的属性,而不是显式添加任何子项。 The way to specify appearance of each item is through the ListView.ItemTemplate
property: 指定每个项目外观的方法是通过ListView.ItemTemplate
属性:
<ListView ItemsSource="{Binding Images}" BorderThickness="0">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Name}" Height="32" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.