问题
Within a ListView
control, how to use the DisplayMemberPath
property when I want to change the ItemContainerStyle
?
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>
Is it possible to solve this in XAML? How is it solved in the original ControlTemplate
?
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>
Is it necessary? Or is it the display value already resolved by the original ListView
control?
回答1:
Is it possible to solve it by XAML?
No, you use either a DisplayMemberPath
or an ItemTemplate
/ItemContainerStyle
but not both.
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.
来源:https://stackoverflow.com/questions/46092303/how-to-use-the-displaymemberpath-in-a-listview-with-an-itemcontainerstyle