In a WPF MVVM (Model-View-ViewModel) application, a Radio Button is bound to a property in the ViewModel using the IsChecked property. The IsChecked property of the Radio Button is set to true if the value of the property in the ViewModel matches the Value property of the Radio Button.

For example, suppose we have a ViewModel with a property Gender of type string. We can bind this property to two Radio Buttons for Male and Female as follows:

<RadioButton Content="Male" IsChecked="{Binding Gender, Converter={StaticResource StringToBooleanConverter}, ConverterParameter='Male'}"/>
<RadioButton Content="Female" IsChecked="{Binding Gender, Converter={StaticResource StringToBooleanConverter}, ConverterParameter='Female'}"/>

Here, we are using a converter StringToBooleanConverter to convert the Gender property value to a boolean value for the IsChecked property of the Radio Button. The ConverterParameter is used to specify the value for each Radio Button.

In the ViewModel, we need to define the Gender property as follows:

private string _gender;
public string Gender
{
    get { return _gender; }
    set 
    { 
        _gender = value; 
        OnPropertyChanged(nameof(Gender)); 
    }
}

Here, OnPropertyChanged is a method that raises the PropertyChanged event to notify the View of the property value change.

When the user selects a Radio Button, the IsChecked property of the selected Radio Button is set to true, and the Gender property in the ViewModel is updated with the Value property of the selected Radio Button.

wpf mvvm RadioButto mode

原文地址: https://www.cveoy.top/t/topic/bpfD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录