模型

  1. public class people
  2. {
  3. public string name{get;set;}
  4. public bool? sex{get;set;}
  5. }

转换器

  1. namespace Helper
  2. {
  3. public class StringRadioConvert : IValueConverter
  4. {
  5. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  6. {
  7. if (value == null || parameter == null)
  8. return false;
  9. string checkvalue = value.ToString();
  10. string targetvalue = parameter.ToString();
  11. bool r = checkvalue.Equals(targetvalue, StringComparison.InvariantCultureIgnoreCase);
  12. return r;
  13. }
  14.  
  15. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  16. {
  17. if (value == null || parameter == null)
  18. return null;
  19. bool usevalue = (bool)value;
  20. if (usevalue)
  21. return parameter.ToString();
  22. return null;
  23. }
  24. }
  25. /// <summary>
  26. /// BOOL TO BOOL
  27. /// </summary>
  28. public class BoolRadioConvert : IValueConverter
  29. {
  30. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  31. {
  32. if (value == null || parameter == null)
  33. return false;
  34.  
  35. bool flag = (bool)value;
  36.  
  37. if ((flag && (string)parameter == "男") || (!flag && (string)parameter == "女"))
  38. {
  39. return true;
  40. }
  41.  
  42. return false;
  43. }
  44.  
  45. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  46. {
  47. if (value == null || parameter == null)
  48. return null;
  49. bool usevalue = (bool)value;
  50. if (!usevalue)
  51. return null;
  52. Dictionary<string, bool> dict = new Dictionary<string, bool>();
  53. dict.Add("男", true);
  54. dict.Add("女", false);
  55.  
  56. return dict[parameter.ToString()];
  57. }
  58. }
  59.  
  60. }

VIEW

  1. <UserControl ......
  2. xmlns:helper="clr-namespace:Helper"......>
  3. <UserControl.Resources>
  4. <helper:StringRadioConvert x:Key="radioStringConverter" />
  5. <helper:BoolRadioConvert x:Key="radioBoolConverter" />
  6. </UserControl.Resources>
  7. <Label VerticalAlignment="Center" HorizontalAlignment="Right" FontWeight="ExtraBlack">用户:</Label>
  8. <RadioButton Content="小陈" GroupName="SeatGroup" IsChecked="{Binding people.name, Converter={StaticResource radioStringConverter}, ConverterParameter='小陈'}"></RadioButton>
  9. <RadioButton Content="小李" GroupName="SeatGroup" IsChecked="{Binding people.name, Converter={StaticResource radioStringConverter}, ConverterParameter='小李'}" ></RadioButton>

  10. <Label VerticalAlignment="Center" HorizontalAlignment="Right" FontWeight="ExtraBlack">输血史:</Label>
  11. <RadioButton Margin="4 0" GroupName="TransfusionGroup" IsChecked="{Binding people.sex , Converter={StaticResource radioBoolConverter}, ConverterParameter='男'}"></RadioButton>
  12. <RadioButton Margin="4 0" GroupName="TransfusionGroup" IsChecked="{Binding people.sex , Converter={StaticResource radioBoolConverter}, ConverterParameter='女'}"></RadioButton>
  13.  
  14. </UserControl>

解析:

name为string类型,转化为bool

sex需定义为bool?类型,否则会出现红框提示,此外,IsChecked是无法直接绑定变量的

WPF RadioButton 转换的更多相关文章

  1. WPF常用转换

    原文 WPF常用转换 以下是代码中常常用到的一些转换,整理如下,后续再不断完善: 1.string和Color的转换 //string转Color (Color)ColorConverter.Conv ...

  2. WPF值转换实例

    WPF绑定功能非常方便,有时候点击某值时在另t一处显示此值的另一表现形式或调用其对应的其它值,用WPF值转换功能会很方便,下面就一LISTBOX和TEXTBLOCK控件,把LISTBOX中的值转换成除 ...

  3. WPF 变量转换的实现

    有时候,我们传入的是一个值,但是真正显示的需要是另一个值,这时候就需要转换.比如我们传入一个枚举值,而不同的枚举值对于的图片是不一样的. 这时候就需要一个转换规则.WPF里面给我们提供了一个接口IVa ...

  4. WPF RadioButton的绑定

    1. 枚举类 public enum EnumDataTypes { Simulation, Test } 2. 枚举型与布尔型的转换 public class EnumToBooleanConver ...

  5. wpf RadioButton控件的一个bug,onpropertychanged后会修改旧属性的值

    测试代码下载:http://files.cnblogs.com/djangochina/RadioButtonBug.zip 从上面列表选择不同的行,再设置下面不同的radiobutton看看结果 b ...

  6. WPF RadioButton & CheckBox Style

    <Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...

  7. ControlTemplate in WPF —— RadioButton

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  8. WPF 颜色转换

    从字符串到画刷: var converter = new System.Windows.Media.BrushConverter(); var brush = (Brush)converter.Con ...

  9. [No0000129]WPF(1/7)开始教程[译]

    概要 在我使用了半年多 WPF 后,是时候写点关于 WPF 基础方面的东西了.我发表了一系列针对具体问题的文章.现在是到了让大家明白为什么说WPF是一款在界面开发上带来革命的产品了. 本文针对初级-中 ...

随机推荐

  1. Mybatis学习记录(五)----Mybatis的动态SQL

    1.  什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...

  2. 如何在 在SharePoint 2013/2010 解决方案中添加 ashx (HttpHandler)

    本文讲述如何在 在SharePoint 2013/2010 解决方案中添加 ashx (HttpHandler). 一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩 ...

  3. 生命游戏/Game of Life的Java实现(转)

    首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...

  4. android文件存储位置切换

    最近有个需求,助手的google卫星地图和OpenCycleMap下载的离线地图数据,要能够在内置存储和外置存储空间之间切换,因为离线瓦片数据非常大,很多户外用户希望将这些文件存储在外置TF卡上,不占 ...

  5. 采用 PAT工具及CSP语言,对一个问题进行自动机 建模

    pat是新加坡国立开发的工具,需要的去官网下http://www.comp.nus.edu.sg/~pat/ ,学了一天,是个不错的自动机验证工具,感觉还不错啊. 验证一个数是否为斐波那契数且为质数 ...

  6. 《Swift开发指南》

    <Swift开发指南> 基本信息 作者: 关东升    赵志荣 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115366245 上架时间:2014-8-5 出版日期:20 ...

  7. 问题解决——cout 输出 CString

    Unicode下 wcout<<strText.GetString()<<endl;

  8. jquery parent和parents,children和find

    parent返回匹配元素的父元素的元素集合:parents返回匹配元素的祖先元素的元素集合. children返回匹配元素的子元素的元素集合:find返回匹配元素的后代元素的元素集合.

  9. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  10. mysql force index() 强制索引的使用

    mysql force index() 强制索引的使用 之前跑了一个SQL,由于其中一个表的数据量比较大,而在条件中有破坏索引或使用了很多其他索引,就会使得sql跑的非常慢... 那我们怎么解决呢? ...