zoukankan      html  css  js  c++  java
  • WPF Radio button的解决方案

     WPF Radio button的解决方案

    WPF中的Radio button有bug,不能进行data binding.
    http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx

    适用于boolean type的.
    1. 一个converter:
    /// <summary>
    /// Reference:
    /// http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx
    /// </summary>
    [ValueConversion(typeof(bool), typeof(bool))]
    public class RadioButtonBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool param = bool.Parse(parameter.ToString());
            if (value == null)
            {
                return false;
            }
            else
            {
                return !((bool)value ^ param);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool param = bool.Parse(parameter.ToString());
            return !((bool)value ^ param); ;
        }

    2. How to use it:

    (1) Add converter in resources:
        <RadioButtonBooleanConverter x:Key="RadioButtonBooleanConverter" />
            
    (2) Use converter in RadioButton:
        <RadioButton GroupName="rbGroupNew"
    IsChecked="{Binding Path=xxx, Mode=TwoWay, Converter={StaticResource RadioButtonBooleanConverter}, ConverterParameter=true}" Content="New" />
        <RadioButton GroupName="rbGroupOld" IsChecked="{Binding Path=xxxx, Mode=TwoWay, Converter={StaticResource RadioButtonBooleanConverter}, ConverterParameter=false}" Content="Old" />

    3. Essential points:
    (1) xxx must be dependency property;
    (2) Radio button group is different, Mode should be "TwoWay", ConverterParameter=true/false;

    4. If Radio buttons states are more than one:
    http://www.wpftutorial.net/RadioButton.html


       

  • 相关阅读:
    autoMapper dotnetcore webapi 自动添加映射 abp
    win10安装MongoDB提示 the domain,user name and/or password are incorrect. Remember to use "." for the domain if the account is on the local machine.
    webapi 重复提交问题
    webapi postman 415 错误
    sqlserver 更新通过 select 查询出的结果集
    2016-03至2016-08前端工作总结
    css笔记——css 实现自定义按钮
    javascript笔记——date以及datetime的比较
    node.js笔记——gulp
    javascript笔记——密码组合规则
  • 原文地址:https://www.cnblogs.com/SunWentao/p/1638954.html
Copyright © 2011-2022 走看看