我有一个通用的样式,适用于 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
被绑定到一个List<T>
的KeyValuePairs
。如果我移除样式,DisplayMemberPath
就能正确显示,所以问题肯定出在样式上。
<ListBox Style="{StaticResource RadioButtonListBoxStyle}"
ItemsSource="{Binding MyCollection}"
DisplayMemberPath="Value" SelectedValuePath="Key" />