我正在使用Visual Studio 2008,我遇到了radiobuttons问题。
我有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;
}
radiobutton绑定的属性:
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");
}
}
}
当我点击radiobutton时,状态正在改变。 问题是,当我想通过点击取消选中一个单选按钮时,状态会发生变化,但仍会检查无线电按钮。
我在我的转换器中放置了断点,函数转换。例如,如果我想取消选中“就绪”,程序将进行2次“报告”和“取消”,但不是“准备好”......
我真的不明白问题出在哪里。 你能解释一下如何修复它吗?
答案 0 :(得分:1)
这是RadioButton
的问题。一旦取消选中,它就会失去绑定。要解决此问题,您可以将RadioButton.Command
Radiobuttons绑定到ViewModel的命令,并发送一个唯一的CommandParameter来识别哪个按钮在commandhandler中调用了该命令。
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/>
并且在命令处理程序中,您可以根据收到的命令参数设置属性,而不是在转换器中进行。