1

I have wpf window which has four radio button choices and submit button. Submit button is binded to StartWork command inside MainWindowViewModel.cs This command points further to the private method to do some work. Problem is that I’m getting selected radio choice properly only for the first time, on same form when changing radio selection to some other choice (other than first selected) I’m getting null as parameter value (string selectedChoice)

MainWindow.xaml

<Window.Resources>        
        <Helpers:BooleanToStringValueConverter x:Key="BoolToStrValueConverter" />        
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
            <Label>STATUS:</Label>
            <Label Content="{Binding Path=Status, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="200" />
        </StackPanel>
        <!-- radio buttons -->
        <StackPanel Orientation="Horizontal" Width="288" Height="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,200,0,0">
            <RadioButton GroupName="Site" x:Name="All" Content="Sve" Width="50" 
                    IsChecked="{Binding Site, ConverterParameter=All, Converter={StaticResource BoolToStrValueConverter}}"/>
            <RadioButton GroupName="Site" x:Name="SelOne" Content="SelOne" Width="90" 
                    IsChecked="{Binding Site, ConverterParameter=SelOne, Converter={StaticResource BoolToStrValueConverter}}"/>
            <RadioButton GroupName="Site" x:Name="SelTwo" Content=" SelTwo " Width="70" 
                    IsChecked="{Binding Site, ConverterParameter= SelTwo, Converter={StaticResource BoolToStrValueConverter}}"/>
            <RadioButton GroupName="Site" x:Name="SelThree" Content=" SelThree " Width="80" 
                    IsChecked="{Binding Site, ConverterParameter= SelThree, Converter={StaticResource BoolToStrValueConverter}}"/>
        </StackPanel>
        <!-- /radio buttons -->
        <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="200, 20, 0, 20">
            <Button Command="{ Binding StartWorkCommand }" Content="Run" Width="100" Height="50" />
        </StackPanel>
    </Grid>

MainWindowViewModel.cs (implements ViewModelBase.cs)

private ICommand _StartWorkCommand;
private string _Status;
    private string _Site;
public string Status {
   get  {
          return _Status;
        }
   set {
         _Status= value;
         OnPropertyChanged("Status ");
       }
}
public string Site
        {
            get { return _Site; }
            set
            {
                _Site = value;
            }
        }
public ICommand StartWorkCommand {
       get {
              if (_StartWorkCommand == null) {
                    _StartWorkCommand = new RelayCommand(
                       x => this.DoWork(this.Site));
              }
              return _StartWorkCommand;
          }
 }

private void DoWork(string selectedChoice){
     … switch …
}

Again, question is: Why I'm getting proper value as parameter on DoWork method only for first selected radio choice, after that any other selection is null?

EDIT

public class BooleanToStringValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (System.Convert.ToString(value).Equals(System.Convert.ToString(parameter)))
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (System.Convert.ToBoolean(value))
            {
                return parameter;
            }
            return null;
        }
    }
user1765862
  • 13,635
  • 28
  • 115
  • 220

1 Answers1

4

The return null; in your ConvertBack function is getting you.

Instead, use Binding.DoNothing:

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (System.Convert.ToBoolean(value))
            {
                return parameter;
            }
            return Binding.DoNothing;
        }

null is a totally valid value for an object, so when the converter runs it is setting the backing property to null. You want it to just ignore the value, so Binding.DoNothing is needed.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117