我创建了一个自定义控件,它只是三个单选按钮,以及一个可以是 、 或 之一的类型Answer
属性。单击单选按钮会更改属性,或者更改属性应该会更改选择了哪个单选按钮。被实现为 DependancyProperty,因此我可以使用它执行数据绑定(源是 EF4 实体的属性)。string
"Yes"
"No"
"N/A"
Answer
Answer
Answer
这一切都很好,除了在一种特殊情况下 - 当数据库中列的值与DependencyProperty.Register()
调用中指定的默认值匹配时。在这种情况下,控件显示时没有选中任何单选按钮!
我已经关闭了调试器跳过属性设置器的选项,并且我知道SetChecks
() 正在被调用,并且应该将IsChecked
单选按钮的属性设置为 true。
如果我更改属性的默认值,则该值将变为未检查的值……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
:
<UserControl x:Class="RovingAuditEntry.Controls.YesNoNotApplicableRadio"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="130">
<StackPanel Orientation="Horizontal" Margin="5">
<RadioButton Content="Yes" Height="16" Name="YesRadio" Click="Radio_Click" />
<RadioButton Content="No" Height="16" Name="NoRadio" Margin="5,0,0,0" Click="Radio_Click" />
<RadioButton Content="N/A" Height="16" Name="NaRadio" Margin="5,0,0,0" Click="Radio_Click" />
</StackPanel>
</UserControl>