【发布时间】:2011-09-20 14:31:13
【问题描述】:
我有一个ListBox 的通用样式,它覆盖ItemTemplate 以使用RadioButtons。效果很好,除非我设置了DisplayMemberPath。然后我只得到ListBox中项目的.ToString()。
我觉得我在这里遗漏了一些简单的东西......有人可以帮我发现它吗?
<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>
我的ListBox 绑定到KeyValuePairs 的List<T>。如果我删除样式,DisplayMemberPath 会正确显示,因此它必须与样式有关。
<ListBox Style="{StaticResource RadioButtonListBoxStyle}"
ItemsSource="{Binding MyCollection}"
DisplayMemberPath="Value" SelectedValuePath="Key" />
【问题讨论】:
-
有点晚了,但是您尝试过
吗?没有将内容属性定义为属性,而是因为 XElement 允许您使用 Contentpresenter-Object,它(在我的情况下是在列表框上)返回 DisplayMemberpath 属性中定义的属性值。未经测试,只是一个想法
标签: wpf templates xaml listbox