本文介绍了如何覆盖我的 ListBox 的 ItemTemplate 并仍然保留 DisplayMemberPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于覆盖 ItemTemplate 以使用 RadioButtonsListBox 的通用样式.它工作得很好,除非我设置了 DisplayMemberPath.然后我只得到 ListBox 中项目的 .ToString().

我觉得我在这里遗漏了一些简单的东西......有人可以帮我发现吗?

<Setter Property="BorderBrush" Value="透明"/><Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle"/><Setter Property="ItemContainerStyle"><Setter.Value><Style TargetType="{x:Type ListBoxItem}" ><Setter Property="Margin" Value="2, 2, 2, 0"/><Setter 属性="模板"><Setter.Value><控制模板><边框背景="透明"><单选按钮Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"IsChecked="{绑定路径=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/></边框></控制模板></Setter.Value></Setter></风格></Setter.Value></Setter></风格>

My ListBox 绑定到 KeyValuePairsList.如果我删除样式,DisplayMemberPath 会正确显示,因此它必须与样式相符.

解决方案

为什么要保留 DisplayMemberPath?它只是包含 TextBlockItemTemplate 的快捷方式,显示 DisplayMemberPath 中的值.使用您自己的 ItemTemplate,您可以更灵活地显示内容和方式.

只需在您的 ItemTemplate 中添加一个 TextBlock 并设置 Text="{Binding Value}" 即可获得所需的内容.>

此处所述:

此属性是定义默认模板的一种简单方法描述如何显示数据对象.

DisplayMemberPath 提供了一种创建模板的简单方法,但您需要另一种方法.你不能也不需要两者.

I have a generic style for a ListBox that overwrites the ItemTemplate to use RadioButtons. It works great, EXCEPT when I set a DisplayMemberPath. Then I just get the .ToString() of the item in the ListBox.

I feel like I'm missing something simple here... can someone help me spot it?

<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton
                                    Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>

                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

My ListBox is bound to a List<T> of KeyValuePairs. If I remove the Style, the DisplayMemberPath shows up correctly so it must be something with the style.

<ListBox Style="{StaticResource RadioButtonListBoxStyle}"
         ItemsSource="{Binding MyCollection}"
         DisplayMemberPath="Value" SelectedValuePath="Key" />
解决方案

Why exactly do you want to keep DisplayMemberPath? Its just a shortcut for an ItemTemplate containing a TextBlock showing the value in DisplayMemberPath. With your own ItemTemplate you have much more flexibility what and how you want to display.

Just add a TextBlock into your ItemTemplate and set Text="{Binding Value}" and you have what you want.

As described here:

DisplayMemberPath provides a simple way to a template, but you want a different one. You can't and you don't need both.

这篇关于如何覆盖我的 ListBox 的 ItemTemplate 并仍然保留 DisplayMemberPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 18:50