Reputation: 913
I am tring to create a simple UserControl that contains a set of RadioButtons and then sets a single DependencyProperty to a char value (each RadioButton has a unique char value associated with it). I'm taking my cue from this article http://wpftutorial.net/RadioButton.html which seemed to be an elegant solution, but I can't get it to work. Checking one of the RadioButtons does not change the Property. Neither of the ValueConverter's methods are ever called. There are no compile-time errors or binding errors at run-time. What am I missing?
Here is my XAML:
<UserControl x:Class="TestClientWpf.OrderTypePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TestClientWpf="clr-namespace:TestClientWpf">
<WrapPanel>
<WrapPanel.Resources>
<TestClientWpf:CharMatchToBooleanConverter x:Key="converter" />
</WrapPanel.Resources>
<RadioButton IsChecked="{Binding Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=1}">Type 1</RadioButton>
<RadioButton IsChecked="{Binding Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=2}">Type 2</RadioButton>
<RadioButton IsChecked="{Binding Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=3}">Type 3</RadioButton>
</WrapPanel>
</UserControl>
My code behind:
public partial class OrderTypePicker
{
public static readonly DependencyProperty OrderTypeProperty = DependencyProperty.Register("OrderType", typeof(char), typeof(OrderTypePicker), new FrameworkPropertyMetadata('1'));
public char OrderType
{
get { return (char)GetValue(OrderTypeProperty); }
set { SetValue(OrderTypeProperty, value); }
}
public OrderTypePicker()
{
InitializeComponent();
}
}
My ValueConverter:
public class CharMatchToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
string checkValue = value.ToString();
string targetValue = parameter.ToString();
return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return null;
bool useValue = (bool)value;
string targetValue = parameter.ToString();
return useValue ? char.Parse(targetValue) : (char?) null;
}
Upvotes: 1
Views: 2467
Reputation: 399
Refer this link if you need explaantion WPF Tutorial
Or you can use
`Radio1.GroupName = Radio2.GroupName = this.GetHashCode().ToString() + Radio1.GroupName;`
Upvotes: 0
Reputation: 913
For the record, a colleague of mine also provided an alternative solution: to set the DataContext property to the UserControl instance (for example in the constructor):
public OrderTypePicker()
{
InitializeComponent();
DataContext = this;
}
Upvotes: 0
Reputation: 21261
In a UserControl your DataContext still points to the normal DataContext of its parent.
You need to bind to properties on the control itself, so:
<RadioButton IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=1}">Type 1</RadioButton>
Upvotes: 1