#358 – 将RadioButton和枚举绑定(Binding a RadioButton to an Enumerated Type)
来源:互联网 发布:域名 购买 编辑:程序博客网 时间:2024/05/22 06:46
你可以通过转换器将RadioButton 控件和一个枚举类型绑定。
XAML代码:
<Window.Resources> <loc:EnumToBooleanConverter x:Key="enumToBooleanConverter" /></Window.Resources> <StackPanel HorizontalAlignment="Center" Margin="15"> <Label Content="Favorite animated character?"/> <RadioButton IsChecked="{Binding Path=FavCharacter, Converter={StaticResource enumToBooleanConverter}, ConverterParameter={x:Static loc:CartoonCharacters.Gumby}}" Content="Gumby"/> <RadioButton IsChecked="{Binding Path=FavCharacter, Converter={StaticResource enumToBooleanConverter}, ConverterParameter={x:Static loc:CartoonCharacters.PinkPanther}}" Content="Pink Panther"/> <RadioButton IsChecked="{Binding Path=FavCharacter, Converter={StaticResource enumToBooleanConverter}, ConverterParameter={x:Static loc:CartoonCharacters.Magoo}}" Content="Mr. Magoo"/></StackPanel>
对应的枚举:
public enum CartoonCharacters{ Gumby, PinkPanther, Magoo}
用于绑定的该枚举类型的属性
public CartoonCharacters FavCharacter { get; set; }
转换器:
public class EnumToBooleanConverter : IValueConverter{ // Convert enum [value] to boolean, true if matches [param] public object Convert(object value, Type targetType, object param, CultureInfo culture) { return value.Equals(param); } // Convert boolean to enum, returning [param] if true public object ConvertBack(object value, Type targetType, object param, CultureInfo culture) { return (bool)value ? param : Binding.DoNothing; }}
原文地址:https://wpf.2000things.com/2011/08/05/358-binding-a-radiobutton-to-an-enumerated-type/
******************************************************译者注**************************************************
上面的例子中,有三个RadioButton 控件,每个RadioButton 控件都和CartoonCharacters 枚举的一个值对应,当FavCharacter 属性的值和某个RadioButton控件对应的值一样的时候,该RadioButton 控件将被选中。
0 0
- #358 – 将RadioButton和枚举绑定(Binding a RadioButton to an Enumerated Type)
- #342 – 将Button 和Command绑定(Binding a Button to a Command)
- 关于WPF RadioButton和CheckBox绑定问题
- radiobutton
- radioButton
- Radiobutton
- RadioButton
- RadioButton
- RadioButton
- radiobutton
- RadioButton
- RadioButton
- RadioButton
- Radiobutton
- RadioButton
- RadioButton
- RadioButton
- RadioButton
- redis之初探
- mock probability with lottery
- Tensorflow学习(一)
- 分布式id生成器,互联网
- [置顶] mongodb系列之-解读journal
- #358 – 将RadioButton和枚举绑定(Binding a RadioButton to an Enumerated Type)
- 软件测试工程师基础技能+
- 请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法。
- 可以进行在线编程的网站
- 【NOIP 2011 Day1 T1】铺地毯(模拟)
- java基本类型(内置类型)取值范围
- STM32之SPI库函数介绍
- 如何使用 Xcode8 进行开发调试
- Material Design下面的Toolbar的一些使用