我有这样的枚举:
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绑定到属性以进行正确的双向绑定?
让我们想象我有:
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton1IsChecked}" />
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton2IsChecked}" />
Run Code Online (Sandbox Code Playgroud)
然后在我的数据源类中,我有:
public bool RadioButton1IsChecked { get; set; }
public bool RadioButton2IsChecked { get; set; }
public enum RadioButtons { RadioButton1, RadioButton2, None }
public RadioButtons SelectedRadioButton
{
get
{
if (this.RadioButtonIsChecked)
return RadioButtons.RadioButton1;
else if (this.RadioButtonIsChecked)
return RadioButtons.RadioButton2;
else
return RadioButtons.None;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以以某种方式将我的单选按钮直接绑定到SelectedRadioButton属性吗?我真的只需要RadioButton1IsChecked和RadioButton2IsChecked属性来计算选定的单选按钮.
使用以下XAML如何在Button的事件处理程序中获取对所选单选按钮的引用?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="myWindow">
<Grid>
<StackPanel>
<RadioButton Content="A" GroupName="myGroup"></RadioButton>
<RadioButton Content="B" GroupName="myGroup"></RadioButton>
<RadioButton Content="C" GroupName="myGroup"></RadioButton>
</StackPanel>
<Button Click="Button_Click" Height="100" Width="100"></Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 单击其中一个时,为什么矩形出现在单选按钮中.

XAML标记如下
<RadioButton GroupName="LabelDisp" IsChecked=".. Converter={StaticResource enumBooleanConverter}, ConverterParameter= LabOnly}" Content="{x:Static resx:StringRes.RadioButtonLab}" Style="{StaticResource ListOption}" Command="{Binding Path=Command}"></RadioButton>
<RadioButton GroupName="LabelDisp" IsChecked=".. Converter={StaticResource enumBooleanConverter}, ConverterParameter= DescOnly}" Content="{x:Static resx:StringRes.RadioButtonDesc}" Style="{StaticResource ListOption}" Command="{Binding Path=Command}"></RadioButton>
<RadioButton GroupName="LabelDisp" IsChecked=".. Converter={StaticResource enumBooleanConverter}, ConverterParameter= LabAndDescr}" Content="{x:Static resx:StringRes.RadioButtonBoth}" Style="{StaticResource ListOption}" Command="{Binding Path=Command}"></RadioButton>
Run Code Online (Sandbox Code Playgroud) <GroupBox x:Name="groupBox" Header="Operating System" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="74" Width="280">
<StackPanel>
<RadioButton GroupName="Os" Content="Windows 7 (64-bit)" IsChecked="True"/>
<RadioButton GroupName="Os" Content="Windows 7 (32-bit)" />
</StackPanel>
</GroupBox>
Run Code Online (Sandbox Code Playgroud)
我的应用程序中有几个单选按钮组
如何使用C#访问Code-Behind中已检查的哪一个?
是否绝对有必要在每个RadioButton上使用x:Name =还是有更好的方法吗?
代码示例总是受到赞赏
wpf ×5
radio-button ×4
xaml ×3
binding ×1
c# ×1
data-binding ×1
enums ×1
radio-group ×1
selecteditem ×1