我有这样的枚举:
public enum MyLovelyEnum
{
FirstSelection,
TheOtherSelection,
YetAnotherOne
};
Run Code Online (Sandbox Code Playgroud)
我的DataContext中有一个属性:
public MyLovelyEnum VeryLovelyEnum { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的WPF客户端中有三个RadioButton.
<RadioButton Margin="3">First Selection</RadioButton>
<RadioButton Margin="3">The Other Selection</RadioButton>
<RadioButton Margin="3">Yet Another one</RadioButton>
Run Code Online (Sandbox Code Playgroud)
现在如何将RadioButtons绑定到属性以进行正确的双向绑定?
我有一个通用的样式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>
Run Code Online (Sandbox Code Playgroud)
我ListBox绑定到List<T>的KeyValuePairs.如果我删除了样式,则DisplayMemberPath显示正确,因此它必须是样式的东西.
<ListBox Style="{StaticResource RadioButtonListBoxStyle}"
ItemsSource="{Binding MyCollection}"
DisplayMemberPath="Value" SelectedValuePath="Key" />
Run Code Online (Sandbox Code Playgroud)