开发者

Binding radiobutton to enum in silverlight

I am using this question/answer as source code for my current need. It works fine but I can select my radiobutton only once. Here is my code :

<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={StaticResourc开发者_如何学编程e EnumToVisibilityConverter}, 
                                ConverterParameter=CarrozzeLocomotive}"
                    Command="{Binding CambioMaterialeCommand}"/>
</StackPanel>

In the ViewModel, I have :

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;
            }

        }

When putting a breakpoint on the switch, I can see the Materiale value changing when going from one radiobutton to the other, but if I try to reselect one radiobutton I already selected, this value does not change and keep to the radiobutton value selected before.

Could someone explain me what is going on?

Thanks for your help,

[Edit]

here is my enum converter :

   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;
    }

[/EDIT]


Here is the link to article that might help you to achive task easily or to resolve your issue: Binding RadioButtons to an Enum in Silverlight

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜