wpf radiobutton 组合绑定 事例
时间: 2023-08-26 14:03:45 浏览: 103
以下是一个WPF的RadioButton组合绑定的示例代码:
```xaml
<StackPanel>
<RadioButton Content="Option 1" IsChecked="{Binding Path=Option1Checked}" />
<RadioButton Content="Option 2" IsChecked="{Binding Path=Option2Checked}" />
<RadioButton Content="Option 3" IsChecked="{Binding Path=Option3Checked}" />
</StackPanel>
```
在此示例中,三个RadioButton分别绑定到ViewModel中的三个bool属性,例如:
```csharp
public class MyViewModel : INotifyPropertyChanged
{
private bool _option1Checked;
public bool Option1Checked
{
get { return _option1Checked; }
set
{
_option1Checked = value;
OnPropertyChanged(nameof(Option1Checked));
}
}
private bool _option2Checked;
public bool Option2Checked
{
get { return _option2Checked; }
set
{
_option2Checked = value;
OnPropertyChanged(nameof(Option2Checked));
}
}
private bool _option3Checked;
public bool Option3Checked
{
get { return _option3Checked; }
set
{
_option3Checked = value;
OnPropertyChanged(nameof(Option3Checked));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
在此示例中,用户可以选择一个选项,ViewModel中对应的属性将自动更新。
阅读全文