我正在使用this问题/答案作为我当前需求的源代码。 它工作正常,但我只能选择一次我的单选按钮。这是我的代码:
<StackPanel Orientation="Vertical" Grid.Column="0" >
<sdk:Label Content="Materiale:" Margin="0,0,0,5" />
<RadioButton GroupName="Materiale" Margin="0,0,0,2"
Content="All"
IsChecked="{Binding Path=Materiale,
Mode=TwoWay,
Converter={StaticResource EnumToVisibilityConverter},
ConverterParameter=All}"
Command="{Binding CambioMaterialeCommand}" />
<RadioButton GroupName="Materiale" Margin="0,0,0,2"
Content="RotabiliSingoli"
IsChecked="{Binding Path=Materiale,
Mode=TwoWay,
Converter={StaticResource EnumToVisibilityConverter},
ConverterParameter=RotabiliSingoli}"
Command="{Binding CambioMaterialeCommand}" />
<RadioButton GroupName="Materiale" Margin="0,0,0,2"
Content="Treni"
IsChecked="{Binding Path=Materiale,
Mode=TwoWay,
Converter={StaticResource EnumToVisibilityConverter},
ConverterParameter=Treni}"
Command="{Binding CambioMaterialeCommand}" />
<RadioButton GroupName="Materiale" Margin="0,0,0,2"
Content="CarrozzeLocomotive"
IsChecked="{Binding Path=Materiale,
Mode=TwoWay,
Converter={StaticResource EnumToVisibilityConverter},
ConverterParameter=CarrozzeLocomotive}"
Command="{Binding CambioMaterialeCommand}"/>
</StackPanel>
在ViewModel中,我有:
public void CambioMateriale()
{
switch (Materiale)
{
case E_Materiale.All:
IsVisibleLocomotivaCarozza = false;
IsVisibleTrenoPartenza = false;
break;
case E_Materiale.RotabiliSingoli:
IsVisibleLocomotivaCarozza = false;
IsVisibleTrenoPartenza = false;
break;
case E_Materiale.Treni:
IsVisibleLocomotivaCarozza = false;
IsVisibleTrenoPartenza = true;
break;
case E_Materiale.CarrozzeLocomotive:
IsVisibleLocomotivaCarozza = true;
IsVisibleTrenoPartenza = false;
break;
}
}
当在开关上放置断点时,我可以看到当从一个单选按钮到另一个单选按钮时Materiale值发生变化,但是如果我尝试重新选择我已选择的一个单选按钮,则该值不会改变并保持为radiobutton值选择之前。
有人可以解释一下发生了什么吗?
感谢您的帮助,
[编辑]
这是我的枚举转换器:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
if (Enum.IsDefined(value.GetType(), value) == false)
return DependencyProperty.UnsetValue;
object parameterValue = Enum.Parse(value.GetType(), parameterString, true);
return parameterValue.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : DependencyProperty.UnsetValue;
}
[/编辑]