欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf RadioButton按钮绑定数据过程

程序员文章站 2022-06-07 17:59:09
...

1、创建数据转换

public class RadioConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? false : value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }

2、视图加上

  <WrapPanel  >
            <TextBlock >品牌:</TextBlock>
            <RadioButton  IsChecked="{Binding Path=FBrand,Mode=TwoWay ,Converter={StaticResource RadioConverter}, ConverterParameter=0}">test1</RadioButton>
            <RadioButton  IsChecked="{Binding Path=FBrand,Mode=TwoWay ,Converter={StaticResource RadioConverter}, ConverterParameter=1}">test2</RadioButton>
            <RadioButton  IsChecked="{Binding Path=FBrand,Mode=TwoWay ,Converter={StaticResource RadioConverter}, ConverterParameter=2}">test3</RadioButton>
            <RadioButton  IsChecked="{Binding Path=FBrand,Mode=TwoWay ,Converter={StaticResource RadioConverter}, ConverterParameter=3}">test4</RadioButton>
        </WrapPanel>

3、viewModel绑定

FBrand=“0”//FBrand="1"//FBrand="2"//FBrand="3"