【Controls】【RadioButton】Radio Button绑定

本文介绍了一个自定义的WPF绑定转换器IsPublishConverter,并展示了如何使用该转换器来实现RadioButton的选择绑定。通过设置不同的ConverterParameter参数,可以控制RadioButton的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Radio Button绑定

 

转换器


    public class IsPublishConverter : IValueConverter
    {
        
public object Convert(object value, Type targetType,
                              
object parameter, CultureInfo culture)
        {
            
string a = value.ToString();
            
string b = parameter.ToString();

            
if (a == b)
                
return true;
            
else
                
return false;
        }

        
public object ConvertBack(object value, Type targetType,
                                  
object parameter, CultureInfo culture)
        {
            
string a = value.ToString();
            
string b = parameter.ToString();

            
if (a == b && b == "True")
                
return true;
            
else if(a==&& b=="1")
                
return true;
            
else
                
return false;
        }

    }



界面xaml中


<UserControl.Resources>资源中加入
   
<my:IsPublishConverter x:Key="myConverter" />
</UserControl.Resources>


界面元素
  
<StackPanel Margin="5" Orientation="Horizontal">
      
<RadioButton Content="共享" Margin="7,0,0,0" GroupName="1" IsChecked="{Binding Path=SelectFilter.FilterMode, Mode=TwoWay, 
                   Converter={StaticResource myConverter},
                   ConverterParameter
=1}"/>
      <RadioButton Content="私有" Margin="7,0,0,0" GroupName="2" IsChecked="{Binding Path=SelectFilter.FilterMode, Mode=TwoWay, 
                   Converter={StaticResource myConverter},
                   ConverterParameter
=0}"/>
  </StackPanel>
  
<TextBlock Margin="5" Text="过滤器类型:" />
  
<StackPanel Margin="5" Orientation="Horizontal">
      
<RadioButton Content="上报" Margin="7,0,0,0" GroupName="3" IsChecked="{Binding Path=SelectFilter.FilterType, Mode=TwoWay, 
                   Converter={StaticResource myConverter},
                   ConverterParameter
=True}"/>
      <RadioButton Content="过滤" Margin="7,0,0,0" GroupName="4" IsChecked="{Binding Path=SelectFilter.FilterType, Mode=TwoWay, 
                   Converter={StaticResource myConverter},
                   ConverterParameter
=False}"/>
  </StackPanel>



 

转载于:https://www.cnblogs.com/ningth/archive/2010/09/28/1837307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值