WPF Radio组的绑定

都是控件编,RadioButtion 简单绑定使用,model.cs下边定义属性

private int _isSuccess;

public int IsSuccess { get { return _isSuccess; } set { _isSuccess = value; } }

打开 Window1.xaml.cs 文档,model.cs 绑定你的 DataContext, 返回 xaml 绑定 RadioButton 控件:

<RadioButton IsChecked="{Binding Path=IsSuccess, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=1}" Content="one" />

<RadioButton IsChecked="{Binding Path=IsSuccess, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=2}" Content="two" />

<RadioButton IsChecked="{Binding Path=IsSuccess, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=3}" Content="three" />

主要属性是Converter和ConverterParameter,Converter是个转换器,使用动态资源中一个Converter类,就是RadioBoolToIntConverter类,ConverterParameter是传递到RadioBoolToIntConverter类参数实现对比

以下是转换器,RadioBoolToIntConverter.cs:

public class RadioBoolToIntConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

int integer = (int)value;

if (integer==int.Parse(parameter.ToString()))

return true;

else

return false;

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

return parameter;

}

}

添加名命空间

xmlns:local="clr-namespace:名命空间"

添加资源

<Window.Resources>

<local:RadioBoolToIntConverter x:Key="radioBoolToIntConverter" />

</Window.Resources>

很简单就绑定在一样。

原文地址:https://www.cnblogs.com/lizeyan/p/3583864.html