如何在带有ItemContainerStyle的ListView中使用DisplayMemberPath? [英] How to use the DisplayMemberPath in a ListView with an ItemContainerStyle?

查看:70
本文介绍了如何在带有ItemContainerStyle的ListView中使用DisplayMemberPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

是否可以通过XAML解决它?

Is it possible to solve it by XAML?

不,您可以使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆