问题描述
在 ListView
控件中,当我想更改 ItemContainerStyle
时如何使用 DisplayMemberPath
属性?
Within a ListView
control, how to use the DisplayMemberPath
property when I want to change the ItemContainerStyle
?
我在控件的 ControlTemplate
中使用了 ListView
控件,并且 DisplayMemberPath
属性是通过 Binding
设置的不受控制.
I used a ListView
control in the ControlTemplate
of my control and the DisplayMemberPath
property is set via Binding
from outside of control.
<ListView ItemsSource="{TemplateBinding ItemsSource}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Item:" />
<TextBlock Text="{Binding}" />
<TextBlock Text=", " />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
是否可以在 XAML 中解决此问题?如何在原始的 ControlTemplate
中解决该问题?
Is it possible to solve this in XAML? How is it solved in the original ControlTemplate
?
我尝试使用 MultiValueConverter
解决该问题,在这里我绑定了Collection Item和 DisplayMemberPath
.
I try to solve it with a MultiValueConverter
, where I bind the Collection Item and DisplayMemberPath
.
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource NameToValueConverter}">
<Binding />
<Binding Path="DisplayMemberPath" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListView}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
有必要吗?还是显示值已经由原始的 ListView
控件解析了?
Is it necessary? Or is it the display value already resolved by the original ListView
control?
推荐答案
不,您可以使用 DisplayMemberPath
或 ItemTemplate
/ ItemContainerStyle
,但不能同时使用两者.
No, you use either a DisplayMemberPath
or an ItemTemplate
/ItemContainerStyle
but not both.
此外,应该将 DisplayMemberPath
属性设置为指定基础数据类的属性名称的 string
.您可以绑定到的不是不是依赖项属性.
Also, the DisplayMemberPath
property is supposed to be set to a string
that specifies a name of a property of the underlying data class. It is not a dependency property that you can bind to.
这篇关于如何在带有ItemContainerStyle的ListView中使用DisplayMemberPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!