模型

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 转换的更多相关文章

  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. Powershell Remove "Limited Access" - 金大昊(jindahao)

    对于有多级web获取getlist会报错:Exception calling “GetList” with “1” argument $SPWeb = Get-SPWeb -Identity http ...

  2. 使用Kindle4rss推送自己感兴趣的博文

    微信是个好东西,信息量超大,正能量的东西居多,但信息过载的滋味也很不好受,浏览了一大堆铺天盖地的信息后,关上手机后大脑又重新回到空白.所以还是喜欢用RSS聚合功能,自己去订阅优秀的博客或新闻,当有更新 ...

  3. OC数组常见操作

    // 数组存放的内容必须是NSObject对象,数组中存放的内容是有序的 // 数组打印输出在输出台是用()包括起来的 // NSArray 不可变数组 // 1.创建对象 // 初始化方法 NSAr ...

  4. 安装VVDocumenter-Xcode-master (Xcode 7.1)的过程

    下载地址: http://pan.baidu.com/s/1boxvewB 1.首先下载解压压缩包打开VVDocumenter工程,编译一遍(快捷键com+B) 2.在finder里面的应用程序,找到 ...

  5. git报错 error: cannot stat ‘file’: Permission denied

    切换分支时报错: error: cannot stat ‘file’: Permission denied 解决方法:退出编辑器.浏览器.资源管理器等,然后再切换就可以了.

  6. 关于C语言中单双引号的问题

    代码 #include<stdio.h> int main() { if ( "{" =='{' ) printf("True\n"); else ...

  7. java网络---查找Internet

    连接到Internet的设备称为节点,计算机节点称为host. 为了区别每一台连接互联网的计算机,就有了Internet Protocol地址的概念. IPV4 & IPV6 我们以前默认的是 ...

  8. C# 3个线程A B C 依次打印123123123..

    C#经典面试题: 有3个线程,A线程打印1,B线程打印2,C线程打印3,请用程序实现依次打印123123123... class Program { static void Main(string[] ...

  9. Flash网站Loading制作

    Flash网站Loading制作~~~ stop(); stage.scaleMode=StageScaleMode.NO_SCALE; //指定舞台属性为不跟随播放器大小而改变 stage.show ...

  10. MYSQL界面操作系统之phpMyAdmin

    linux下: 需要PHP环境支持,安装PHP自行百度 下载linux-phpMyAdmin,并解压 php -S 127.0.0.1:8081 -t phpMyAdmin/