WPF RadioButton 转换
模型
- public class people
- {
- public string name{get;set;}
- public bool? sex{get;set;}
- }
转换器
- namespace Helper
- {
- public class StringRadioConvert : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- if (value == null || parameter == null)
- return false;
- string checkvalue = value.ToString();
- string targetvalue = parameter.ToString();
- bool r = checkvalue.Equals(targetvalue, StringComparison.InvariantCultureIgnoreCase);
- return r;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value == null || parameter == null)
- return null;
- bool usevalue = (bool)value;
- if (usevalue)
- return parameter.ToString();
- return null;
- }
- }
- /// <summary>
- /// BOOL TO BOOL
- /// </summary>
- public class BoolRadioConvert : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- if (value == null || parameter == null)
- return false;
- bool flag = (bool)value;
- if ((flag && (string)parameter == "男") || (!flag && (string)parameter == "女"))
- {
- return true;
- }
- return false;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value == null || parameter == null)
- return null;
- bool usevalue = (bool)value;
- if (!usevalue)
- return null;
- Dictionary<string, bool> dict = new Dictionary<string, bool>();
- dict.Add("男", true);
- dict.Add("女", false);
- return dict[parameter.ToString()];
- }
- }
- }
VIEW
- <UserControl ......
- xmlns:helper="clr-namespace:Helper"......>
- <UserControl.Resources>
- <helper:StringRadioConvert x:Key="radioStringConverter" />
- <helper:BoolRadioConvert x:Key="radioBoolConverter" />
- </UserControl.Resources>
- <Label VerticalAlignment="Center" HorizontalAlignment="Right" FontWeight="ExtraBlack">用户:</Label>
- <RadioButton Content="小陈" GroupName="SeatGroup" IsChecked="{Binding people.name, Converter={StaticResource radioStringConverter}, ConverterParameter='小陈'}"></RadioButton>
- <RadioButton Content="小李" GroupName="SeatGroup" IsChecked="{Binding people.name, Converter={StaticResource radioStringConverter}, ConverterParameter='小李'}" ></RadioButton>
<Label VerticalAlignment="Center" HorizontalAlignment="Right" FontWeight="ExtraBlack">输血史:</Label>- <RadioButton Margin="4 0" GroupName="TransfusionGroup" IsChecked="{Binding people.sex , Converter={StaticResource radioBoolConverter}, ConverterParameter='男'}">男</RadioButton>
- <RadioButton Margin="4 0" GroupName="TransfusionGroup" IsChecked="{Binding people.sex , Converter={StaticResource radioBoolConverter}, ConverterParameter='女'}">女</RadioButton>
- </UserControl>
解析:
name为string类型,转化为bool
sex需定义为bool?类型,否则会出现红框提示,此外,IsChecked是无法直接绑定变量的
WPF RadioButton 转换的更多相关文章
- WPF常用转换
原文 WPF常用转换 以下是代码中常常用到的一些转换,整理如下,后续再不断完善: 1.string和Color的转换 //string转Color (Color)ColorConverter.Conv ...
- WPF值转换实例
WPF绑定功能非常方便,有时候点击某值时在另t一处显示此值的另一表现形式或调用其对应的其它值,用WPF值转换功能会很方便,下面就一LISTBOX和TEXTBLOCK控件,把LISTBOX中的值转换成除 ...
- WPF 变量转换的实现
有时候,我们传入的是一个值,但是真正显示的需要是另一个值,这时候就需要转换.比如我们传入一个枚举值,而不同的枚举值对于的图片是不一样的. 这时候就需要一个转换规则.WPF里面给我们提供了一个接口IVa ...
- WPF RadioButton的绑定
1. 枚举类 public enum EnumDataTypes { Simulation, Test } 2. 枚举型与布尔型的转换 public class EnumToBooleanConver ...
- wpf RadioButton控件的一个bug,onpropertychanged后会修改旧属性的值
测试代码下载:http://files.cnblogs.com/djangochina/RadioButtonBug.zip 从上面列表选择不同的行,再设置下面不同的radiobutton看看结果 b ...
- WPF RadioButton & CheckBox Style
<Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...
- ControlTemplate in WPF —— RadioButton
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- WPF 颜色转换
从字符串到画刷: var converter = new System.Windows.Media.BrushConverter(); var brush = (Brush)converter.Con ...
- [No0000129]WPF(1/7)开始教程[译]
概要 在我使用了半年多 WPF 后,是时候写点关于 WPF 基础方面的东西了.我发表了一系列针对具体问题的文章.现在是到了让大家明白为什么说WPF是一款在界面开发上带来革命的产品了. 本文针对初级-中 ...
随机推荐
- Mybatis学习记录(五)----Mybatis的动态SQL
1. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...
- 如何在 在SharePoint 2013/2010 解决方案中添加 ashx (HttpHandler)
本文讲述如何在 在SharePoint 2013/2010 解决方案中添加 ashx (HttpHandler). 一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩 ...
- 生命游戏/Game of Life的Java实现(转)
首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...
- android文件存储位置切换
最近有个需求,助手的google卫星地图和OpenCycleMap下载的离线地图数据,要能够在内置存储和外置存储空间之间切换,因为离线瓦片数据非常大,很多户外用户希望将这些文件存储在外置TF卡上,不占 ...
- 采用 PAT工具及CSP语言,对一个问题进行自动机 建模
pat是新加坡国立开发的工具,需要的去官网下http://www.comp.nus.edu.sg/~pat/ ,学了一天,是个不错的自动机验证工具,感觉还不错啊. 验证一个数是否为斐波那契数且为质数 ...
- 《Swift开发指南》
<Swift开发指南> 基本信息 作者: 关东升 赵志荣 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115366245 上架时间:2014-8-5 出版日期:20 ...
- 问题解决——cout 输出 CString
Unicode下 wcout<<strText.GetString()<<endl;
- jquery parent和parents,children和find
parent返回匹配元素的父元素的元素集合:parents返回匹配元素的祖先元素的元素集合. children返回匹配元素的子元素的元素集合:find返回匹配元素的后代元素的元素集合.
- CI 框架中的自定义路由规则
在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...
- mysql force index() 强制索引的使用
mysql force index() 强制索引的使用 之前跑了一个SQL,由于其中一个表的数据量比较大,而在条件中有破坏索引或使用了很多其他索引,就会使得sql跑的非常慢... 那我们怎么解决呢? ...