Как я могу переписать ItemTemplate моего ListBox и при этом сохранить DisplayMemberPath?
У меня есть общий стиль дляListBox
который перезаписываетItemTemplate
использоватьRadioButtons
, Это прекрасно работает, за исключением, когда я установилDisplayMemberPath
, Тогда я просто получаю.ToString()
предмета вListBox
.
Я чувствую, что здесь упускаю что-то простое ... Может ли кто-нибудь помочь мне определить это?
<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" />