单选按钮,绑定,转换器 [英] RadioButton, Binding, Converters
问题描述
我正在使用Visual Studio 2008,但是单选按钮有问题.
I am working on Visual Studio 2008, and I have a problem with radiobuttons.
我有3个radioButton:
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>
转换器:
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.
我在转换器中设置了断点,即功能Convert.例如,如果我要取消选中就绪",则该程序将执行两次,分别是已报告"和已取消",而不是就绪" ...
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 ?
推荐答案
这是RadioButton
的问题.一旦取消检查,它将失去其绑定.要解决此问题,可以将Radiobuttons的RadioButton.Command
绑定到ViewModel的命令,并发送唯一的CommandParameter来标识哪个按钮在commandhandler中调用了该命令.
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.
这篇关于单选按钮,绑定,转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!