views:

85

answers:

1

Trying to get radiobuttons binding working but getting a run time error with the code below. Want the radio buttons to act such that only one can be selected at a time, and that they bind correctly in a 2 way fashion. Error text is.

"The invocation of the constructor on type 'testapp1.MainWindow' that matches the specified binding constraints threw an exception"

<Window x:Class="testapp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:l="clr-namespace:testapp1" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <l:EnumBooleanConverter x:Key="enumBooleanConverter" />
        </Grid.Resources>

        <StackPanel >
            <RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=FirstSelection}">first selection</RadioButton>
            <RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=TheOtherSelection}">the other selection</RadioButton>
            <RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=YetAnotherOne}">yet another one</RadioButton>
            <Label Content="{Binding Path=VeryLovelyEnum}" Height="28" Name="label1" />
        </StackPanel>

    </Grid>
</Window>

And code:

namespace testapp1
{
    public partial class MainWindow : Window
    {
        public TestModel _model;

        public MainWindow()
        {
            InitializeComponent();

            InitializeComponent();
            _model = new TestModel();
            this.DataContext = _model;
        }

    }

    public enum MyLovelyEnum
    {
        FirstSelection,
        TheOtherSelection,
        YetAnotherOne
    };


    public class TestModel : DependencyObject
    {

        public MyLovelyEnum VeryLovelyEnum
        {
            get { return (MyLovelyEnum)GetValue(VeryLovelyEnumProperty); }
            set { SetValue(VeryLovelyEnumProperty, value); }
        }
        public static readonly DependencyProperty VeryLovelyEnumProperty =
            DependencyProperty.Register("VeryLovelyEnum", typeof(MyLovelyEnum), typeof(TestModel), new UIPropertyMetadata(0));



    // from http://stackoverflow.com/questions/397556/wpf-how-to-bind-radiobuttons-to-an-enum
    public class EnumBooleanConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string parameterString = parameter as string;
            if (parameterString == null)
                return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false)
                return DependencyProperty.UnsetValue;

            object parameterValue = Enum.Parse(value.GetType(), parameterString);

            return parameterValue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string parameterString = parameter as string;
            if (parameterString == null)
                return DependencyProperty.UnsetValue;

            return Enum.Parse(targetType, parameterString);
        }
        #endregion
    }

}

The button & label is there from a previous test I did - I just left in...

+2  A: 

The following declaration uses an integer instead of a default enum-value. Could be your instantiation problem...

public static readonly DependencyProperty VeryLovelyEnumProperty = 
            DependencyProperty.Register("VeryLovelyEnum", typeof(MyLovelyEnum), typeof(TestModel), new UIPropertyMetadata(0)); 

Try something like...

public static readonly DependencyProperty VeryLovelyEnumProperty = 
            DependencyProperty.Register("VeryLovelyEnum", typeof(MyLovelyEnum), typeof(TestModel), new UIPropertyMetadata(MyLovelyEnum.FirstSelection)); 
HCL