2

I think I've followed the examples given in this post but my property is not changing when button are changed. Any suggestions on where I went wrong?

C# code for enum and class

public enum SystemTypes
{
    TypeA,
    TypeB
}

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }
    SystemTypes systemType = SystemTypes.TypeA;
    public SystemTypes SystemType 
    {
        get { return systemType; }
        set { systemType = value; }
    }
}

public class EnumToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}

xaml

        <Canvas>
            <Canvas.Resources>
                <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />
            </Canvas.Resources>
            <RadioButton x:Name="TypeARadioButton" Content="TypeA" Canvas.Left="10" Canvas.Top="10" 
                         IsChecked="{Binding Path=SystemType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:SystemTypes.TypeA}}" />
            <RadioButton x:Name="TypeBRadioButton" Content="TypeB" Canvas.Left="10" Canvas.Top="31"
                         IsChecked="{Binding Path=SystemType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:SystemTypes.TypeB}}" />

        </Canvas>
Community
  • 1
  • 1
James R
  • 145
  • 2
  • 13
  • You didn't set DataContext for the window so it can't find `SystemType` property to bind to. – icebat Aug 21 '14 at 10:57

1 Answers1

1

You need to set Binding Mode to TwoWay, then in Converter implement method ConvertBack responsible for converting bool to SystemTypes, in settter of SystemType include

set { systemType = value; OnPropertyChanged(() => "SystemType");}

in order to fill property in that its value was changed.

OnPropertyChanged(() => "SystemType")

can work if you implement interface INotifyPropertyChanged. I cannot you whether you set DataContext, if you did not binding is not working. In order to rectify this after InitializeComponent() add

this.DataContext = this;
Maximus
  • 3,458
  • 3
  • 16
  • 27
  • Sorry forgot to add the Converter code. I edited the question to show that class also, I had also tried two way. The code you specified won't compile because the lambda expression is not a delegate type, but I don't think it matters because the setter is never being called. – James R Aug 21 '14 at 02:10