- I'm having few RadioButtons and I don't want to bind "IsChecked" property of each one of them to unique property in the code.
- I want to have a single property like "CurrentSelected" and according to that to set the "IsChecked".
- In addition I don't want to use converters.
I tried to use the behavior "ChangePropertyAction" but it looks like it's working only in one way. here is my code:
<RadioButton x:Name="UpRadioButton" Margin="5" Content="Up" > <i:Interaction.Triggers> <ei:DataTrigger Binding="{Binding IsChecked, ElementName=UpRadioButton}" Value="True"> <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Up}" /> </ei:DataTrigger> </i:Interaction.Triggers> </RadioButton> <RadioButton x:Name="DownRadioButton" Margin="5" Content="Down" > <i:Interaction.Triggers> <ei:DataTrigger Binding="{Binding IsChecked, ElementName=DownRadioButton}" Value="True"> <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Down}" /> </ei:DataTrigger> </i:Interaction.Triggers> </RadioButton> <RadioButton x:Name="LeftRadioButton" Margin="5" Content="Left" > <i:Interaction.Triggers> <ei:DataTrigger Binding="{Binding IsChecked, ElementName=LeftRadioButton}" Value="True"> <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Left}" /> </ei:DataTrigger> </i:Interaction.Triggers> </RadioButton> <RadioButton x:Name="RightRadioButton" Margin="5" Content="Right" > <i:Interaction.Triggers> <ei:DataTrigger Binding="{Binding IsChecked, ElementName=RightRadioButton}" Value="True"> <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Right}" /> </ei:DataTrigger> </i:Interaction.Triggers> </RadioButton>
my view model is very simple: MainViewModel.cs
public class MainViewModel : ViewModelBase
{
private DirectionEnum _selectedDirection;
public DirectionEnum SelectedDirection
{
get { return _selectedDirection; }
set
{
if (_selectedDirection != value)
{
_selectedDirection = value;
RaisePropertyChanged();
}
}
}
public MainViewModel()
{
SelectedDirection = DirectionEnum.Up;
}
}
as you can see from the code, the "Up" RadioButton should be already checked... What am I missing ?
ValueEqualsConverter
is extremely elegant/easy. – Och