如果数据绑定与默认值相同,则单选按钮不会检查单选按钮

时间:2012-12-20 14:16:17

标签: wpf data-binding entity-framework-4 custom-controls

我创建了一个自定义控件,它只是三个单选按钮,以及Answer类型的string属性,可以是"Yes""No"或{{}}之一{1}}。单击单选按钮可更改"N/A"属性,或更改Answer属性应更改选择的单选按钮。 Answer实现为DependancyProperty,因此我可以使用它执行数据绑定(源是EF4实体的属性)。

除了在一个特定情况下 - 当数据库中列的值与Answer调用中指定的默认值匹配时,一切正常且良好。在这种情况下,显示控件时不选中任何单选按钮!

我已经关闭调试器的选项以跳过属性设置器,我知道正在调用DependencyProperty.Register()()并且假设将单选按钮的SetChecks属性设置为true。

如果我更改了属性的默认值,那将成为未检查的值... IsChecked已经到位,只是为了识别我在调试器中看到的特定控件,因为我有我认为这些控件中有多个。

首先是MyId代码:

.cs

以下是public partial class YesNoNotApplicableRadio : UserControl, INotifyPropertyChanged { public string MyId { get; set; } public static readonly DependencyProperty AnswerProperty = DependencyProperty.Register( "Answer", typeof(string), typeof(YesNoNotApplicableRadio), new FrameworkPropertyMetadata("N/A", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(AnswerPropertyChanged), new CoerceValueCallback(CoerceAnswer) ), new ValidateValueCallback(ValidateAnswer) ); public static bool ValidateAnswer(Object value) { string s = value as string; if (s == null) return true; s = s.Trim().ToUpper(); if (s.Equals("YES") || s.Equals("NO") || s.Equals("N/A") || s.Equals("")) return true; return false; } public static void AnswerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { YesNoNotApplicableRadio o = d as YesNoNotApplicableRadio; string val = (string)e.NewValue; SetChecks(o, val.Trim().ToUpper()); } private static void SetChecks(YesNoNotApplicableRadio selector, string val) { selector.YesRadio.IsChecked = false; selector.NoRadio.IsChecked = false; selector.NaRadio.IsChecked = false; if (val.Equals("YES")) selector.YesRadio.IsChecked = true; else if (val.Equals("NO")) selector.NoRadio.IsChecked = true; else if (val.Equals("N/A") || val.Equals("")) selector.NaRadio.IsChecked = true; } private void Radio_Click(object sender, RoutedEventArgs e) { if ((bool)YesRadio.IsChecked) Answer = "Yes"; else if ((bool)NoRadio.IsChecked) Answer = "No"; else Answer = "N/A"; } public static Object CoerceAnswer(DependencyObject d, Object baseValue) { YesNoNotApplicableRadio o = (YesNoNotApplicableRadio)d; string s = baseValue as string; if (s == null) return ("N/A"); else return (s); } public string Answer { get { return ((string)GetValue(AnswerProperty)); } set { SetValue(AnswerProperty, value); NotifyPropertyChanged("Answer"); } } public YesNoNotApplicableRadio() { InitializeComponent(); Guid groupGuid = System.Guid.NewGuid(); YesRadio.GroupName = groupGuid.ToString(); NoRadio.GroupName = groupGuid.ToString(); NaRadio.GroupName = groupGuid.ToString(); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } }

.xaml

1 个答案:

答案 0 :(得分:0)

我想出了一个解决方案,虽然它可能不是正确的解决方案。对象的有效输入为null,空字符串"Yes""No""N/A"(任何大小写)。我的强制函数会将null和空字符串转换为"N/A"。通过将传递给Register()函数的默认值从"N/A"更改为"",它现在可以正常工作。

仍在寻找更好的答案,但如果没有,我会接受我自己的答案。