The problem with my code is that RadioButton
's binding and does not allow the delegate to modify the property. Delegate it changes, and binding, it changes to the old value. I am need to be able to change property by command and RadioButton
s.
<Window.InputBindings>
<KeyBinding Key="F1" Command="{Binding SomeCommand}"/>
</Window.InputBindings>
<StackPanel>
<TextBlock Text="{Binding Path=SomeProperty}"/>
<RadioButton IsChecked="{Binding Path=SomeProperty, Mode=TwoWay, Converter={StaticResource ETBConverter}, ConverterParameter=State1}" Content="State1"/>
<RadioButton IsChecked="{Binding Path=SomeProperty, Mode=TwoWay, Converter={StaticResource ETBConverter}, ConverterParameter=State2}" Content="State2"/>
</StackPanel>
public enum TestEnum
{
State1,
State2,
}
public class TestViewModel : BaseViewModel
{
private TestEnum _someProperty;
public TestEnum SomeProperty
{
get { return _someProperty; }
set
{
if (_someProperty != value)
{
_someProperty = value;
OnPropertyChanged();
}
}
}
public Command SomeCommand { get; private set; }
public TestViewModel()
{
_someProperty = TestEnum.State2;
SomeCommand = new Command(SomeCommand_Execute);
}
private void SomeCommand_Execute(object obj)
{
SomeProperty = SomeProperty == TestEnum.State1 ? TestEnum.State2 : TestEnum.State1;
}
}
Update 1:
[Localizability(LocalizationCategory.NeverLocalize)]
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return false;
if (Enum.IsDefined(value.GetType(), value) == false)
return false;
object parameterValue = Enum.Parse(value.GetType(), parameterString);
return parameterValue.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
return Enum.Parse(targetType, parameterString);
}
}
public abstract class BaseViewModel : NotifyPropertyChanged
{
protected Dispatcher UIDispatcher;
public BaseViewModel()
{
UIDispatcher = Dispatcher.CurrentDispatcher;
}
protected void InvokeInUIThread(Action action)
{
if (Thread.CurrentThread == UIDispatcher.Thread)
action();
else
UIDispatcher.InvokeAsync(action, DispatcherPriority.Send);
}
}
public abstract class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Command : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public Command(Action<object> execute)
: this(execute, null)
{
}
public Command(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void OnCanExecutedChanged()
{
CommandManager.InvalidateRequerySuggested();
}
#endregion // ICommand Members
}