I have some problem with binding my RadioButton
property IsChecked
. I have two RadioButton
's on the grid, which Visibility
binded to a property on my viewmodel. What i want to achieve is to always setting first RadioButton
to Checked state, when grid is become visible.
Here is some code:
<Grid Visibility="{Binding State, Converter={StaticResource VisibilityConverter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<RadioButton Grid.Row="0"
Margin="20,0"
IsChecked="{Binding State, Converter={StaticResource StateToBooleanConverter}}"
Content="content 1" />
<RadioButton Grid.Row="1"
Margin="20,0"
Content="content 2" />
</Grid>
Following my logic it should set first RadioButton
as Checked when property State
is going to specific state, when grid is become visible. And its working fine until i hit second RadioButton
. Then my binding is not working and when State
is changing nothing happens in my StateToBooleanConverter
.
I read a lot of information about problems with binding in radiobuttons, but nothing worked in my case.
Is it possible do it without new property for checking radioButton? I would be appreciated for any advise how i can fix this issue.
Edit:
There is some code from viewmodel and Converter for IsChecked
:
public class MainViewModel : ViewModel
{
public MainViewModel
{
this.ChangeState = new RelayCommand(this.ChangeStateExecute);
}
public PageState State
{
get
{
return this.state;
}
set
{
if (this.state != value)
{
this.state = value;
base.RaisePropertyChanged();
}
}
}
public RelayCommand ChangeState { get; private set; }
private void ChangeStateExecute()
{
this.State = PageState.RadioButtonsVisible;
}
}
public class StateToBooleanConverter : Converter
{
protected override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var state = (PageState)value;
var result = state == PageState.RadioButtonsVisible;
return result;
}
}