How to use the DisplayMemberPath in a ListView with an ItemContainerStyle?

只谈情不闲聊 提交于 2021-02-08 10:21:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!