问题
I am working on Visual Studio 2008, and I have a problem with radiobuttons.
I have 3 radioButton :
<Window.Resources>
// [...]
<DataTemplate x:Key="gridViewReadyTemplate">
<StackPanel>
<RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=ready}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="gridViewReportedTemplate">
<StackPanel>
<RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=reported}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="gridViewCanceledTemplate">
<StackPanel>
<RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=canceled}" />
</StackPanel>
</DataTemplate>
// [...]
<ListView Margin="82,133.32,342.5,0" Name="listView1" ItemsSource="{Binding CurrentTrain.PSCommandCollection, Mode=TwoWay}" Height="111.25" VerticalAlignment="Top">
<ListView.View>
<GridView>
// [...]
<GridViewColumn Header="Préparé" Width="50" CellTemplate="{StaticResource gridViewReadyTemplate }" />
<GridViewColumn Header="Reporté" Width="50" CellTemplate="{StaticResource gridViewReportedTemplate }" />
<GridViewColumn Header="Annulé" Width="50" CellTemplate="{StaticResource gridViewCanceledTemplate }" />
</GridView>
</ListView.View>
</ListView>
The converter :
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string param = (string)parameter;
Enumerators.State state = (Enumerators.State)value;
switch (param)
{
case "ready":
if (state == Enumerators.State.READY)
return true;
return false;
case "reported":
if (state == Enumerators.State.REPORTED)
return true;
return false;
case "canceled":
if (state == Enumerators.State.CANCELED)
return true;
return false;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string param = (string)parameter;
if ((bool?)value == true)
{
switch (param)
{
case "ready":
return Enumerators.State.READY;
case "reported":
return Enumerators.State.REPORTED;
case "canceled":
return Enumerators.State.CANCELED;
}
}
return Enumerators.State.NONE;
}
And the property where radiobutton are binding on :
private Enumerators.State commandState;
public Enumerators.State CommandState
{
get { return commandState; }
set
{
if (commandState != value)
{
commandState = value;
NotifyPropertyChanged("CommandState");
}
else
{
commandState = Enumerators.State.NONE;
NotifyPropertyChanged("CommandState");
}
}
}
When I click on a radiobutton, the state is changing well. The problem is when I want to uncheck a radiobutton by clicking on it, the state changes, but the radiobutton still checked.
I put breakpoint in my converter, function Convert. For example, if I want to uncheck "ready", the program go in 2 times, for "reported" and "canceled", but not for "ready"...
I really don't understand where is the problem. Can you explain me how to fix it ?
回答1:
This is the problem with RadioButton
. It loses its binding once it gets unchecked. To fix this, you can bind RadioButton.Command
of Radiobuttons to a command of your ViewModel and send a unique CommandParameter to identify which button has called the command in commandhandler.
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/>
and in the command handler you can set the property depending on the command parameter received instead of doing it in converter.
来源:https://stackoverflow.com/questions/23823844/radiobutton-binding-converters