0

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={StaticResource 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]

Community
  • 1
  • 1
Arthis
  • 2,283
  • 21
  • 32
  • if I understand this question at all you ask why the value doesn't change when you do not change the selected radiobutton (reselect the allready selected)? ... I guess I understand wrong so can you please explain? – Random Dev Aug 30 '11 at 08:37
  • No, I select Radio A and I get value A, I select Radio B and I get B, But If I reselect Radio A, I still have B. Only one radiobutton should be selected at the time. Is it clearer? – Arthis Aug 30 '11 at 08:40
  • Found it it was the converter that was not doing its work properly. – Arthis Aug 30 '11 at 08:54

1 Answers1

1

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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263