在silverlight中将单选按钮绑定到enum
本文关键字:绑定 enum 单选按钮 silverlight | 更新日期: 2023-09-27 18:06:55
我使用这个问题/答案作为我当前需要的源代码。它工作良好,但我只能选择我的单选按钮一次。下面是我的代码:
<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值在从一个单选按钮到另一个单选按钮时发生变化,但是如果我尝试重新选择一个我已经选择的单选按钮,则此值不会改变并保持之前选择的单选按钮值。
谁能给我解释一下这是怎么回事?谢谢你的帮助,
[编辑]这是我的enum转换器:
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;
}
(/编辑)
这篇文章的链接可能会帮助你轻松完成任务或解决你的问题:在Silverlight中将单选按钮绑定到Enum