当前位置: 动力学知识库 > 问答 > 编程问答 >

wpf - Custom control made of radio buttons not checking radio button if data binding is same as default

问题描述:

I created a custom control, which is simply three radio buttons, and an Answer property of type string that can be one of "Yes", "No", or "N/A". Clicking the radio buttons changes the Answer property, or changing the Answer property should change which radio button is selected. Answer is implemented as a DependancyProperty so I can perform data binding with it (the source is a property of an EF4 entity).

It all works well and good, except in one particular situation - when the value of the column in the database matches the default specified in the DependencyProperty.Register() call. In this case, the control is displayed without any radio button checked!

I've turned off the option for the debugger to step over property setters, and I know that SetChecks() is being called and supposedly setting the IsChecked property of the radio button to true.

If I change the default of the property, that becomes the value that is not checked... MyId was put in place just to identify the specific control I'm looking at in the debugger, since I have multiple of these controls in my view.

First the .cs code:

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));

}

}

Here is the .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>

网友答案:

I figured out a solution, although it's probably not the right one. Valid inputs to the control are null, an empty string, "Yes", "No", and "N/A" (with any capitalization). My coerce function will transform null and empty string to "N/A". By changing the default value passed to the Register() function from "N/A" to "", it now works.

Still looking for a better answer, but in the absence of one, I'll accept my own.

分享给朋友:
您可能感兴趣的文章:
随机阅读: