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是一款在界面开发上带来革命的产品了. 本文针对初级-中 ...
随机推荐
- Office 365 - Windows PowerShell for SharePoint Online
Office 365 PowerShell for SharePoint Online 1. Get-SPOTenantLogEntry 并不能获取所有的Log信息,只能用于获取因外部资源而出错的lo ...
- App开发流程之Xcode配置和本地化
补充一点遗漏的Xcode配置. 1.偏好设置.Xcode的菜单栏Xcode -> Preference Fonts & Colors可以自定义编码区和控制台的背景.字体. Text Ed ...
- 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程
Mac隐藏壁纸怎么查找?Mac存不存在隐藏壁纸呢?今天小编就来解决大家的疑问,告诉大家怎么把Mac系统的隐藏壁纸找出来,并且弄能够正常的使用,小编特意写了一个详细的图文教程叫大家如何查找,使用Mac隐 ...
- Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions
Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift ...
- bootstrap3 兼容IE8浏览器
近期在使用bootstrap这个优秀的前端框架,这个框架非常强大,框架里面有下拉菜单.按钮组.按钮下拉菜单.导航.导航条.面包屑.分页.排版.缩略图.警告对话框.进度条.媒体对象等,bootstrap ...
- eclipse智能提示
原文地址:http://www.cnblogs.com/myitm/archive/2010/12/17/1909194.html Windows→Preferences→Java→Editor→Co ...
- 你连Bug都抓不住,还谈什么参与感?
林子大了什么鸟都有,APP市场也是这样.举个例子,有段时期图片社交井喷式发展,各类图片社交APP一时充斥着市场.各种或重视图片加工或主打社交元素的APP“来得快去得快”.“你方唱罢我登场”,这些短命A ...
- 简单的Windows登陆界面设计
要求: 1.用户名必须为字母. //限定用户名必须为字母 private void txtName_KeyPress(object sender, KeyPressEventArgs e) { if ...
- MySQL添加字段和删除字段
MySQL添加字段应该如何实现呢?这是很多刚刚接触MySQL数据库的新人都提到过的问题,下面就为您介绍MySQL添加字段和删除字段的方法,希望对您能有所启迪. MySQL添加字段: alter tab ...
- rpm安装rpm-package报错:Header signature NOKEY 和 error: Failed dependencies:
以前经常遇到这个问题,一直未有记录,今天记录下来: 在安装rpm包的时候报错误如下: Question 1: warning: *.rpm: Header V3 DSA signature: NOKE ...