-1

I have a radiobutton group which I binding to a boolean value but it isn't being picked up in the XAML - am sure it's something simple I am missing - any pointers appreciated.

passed is set to false.

XAML

<RadioButton Width="64" 
      IsChecked="{Binding passed, Converter={StaticResource BoolInverterConverter}}" 
      GroupName="Result">Yes</RadioButton>
<RadioButton Width="64" 
      IsChecked="{Binding passed, Converter={StaticResource BoolInverterConverter}}" 
      GroupName="Result">No</RadioButton>

BoolInverterConverter:

public class BoolInverterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            return !(bool)value;
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            return !(bool)value;
        }

        return value;
    }
}

Not populated:

enter image description here

ViewModel:-

  public ResultsViewModel()
  {
   private Results_results = new Results();

   public ResultsViewModel()
   {
      _results.Passed= false;
   }
  }

Result class:-

    public class Results
    {
      private bool passed;

      public bool Passed{ get => passed; set => passed= value; }
    }
Ram
  • 527
  • 1
  • 10
  • 26
  • Can you post your ViewModel ? And you can also check the Visual Studio output tab while debugging, it will tell you if a binding isn't found. – Seb Dec 24 '20 at 14:03
  • @Seb - ViewModel added - debug not showing failed binding... – Ram Dec 24 '20 at 14:34
  • 2
    Remove one of your converters. Presumably the yes. You're inverting both. That can't be right. – Andy Dec 24 '20 at 14:58

2 Answers2

1
<RadioButton Width="64" IsChecked="{Binding Passed}" GroupName="T1" Style="{DynamicResource CaseSummaryOptions}">Yes</RadioButton>
<RadioButton Width="64" IsChecked="{Binding Passed, Converter={StaticResource InverseBoolRadioConverter}}" GroupName="T1">No</RadioButton>
Ram
  • 527
  • 1
  • 10
  • 26
0

You need to tell the binding when you're updating your viewmodel fields. And you can do it by implementing the INotifyPropertyChanged interface.

Let's make a base class for that:

class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged ( [CallerMemberName] string propertyName = "" )
    {
        PropertyChanged?.Invoke ( this, new PropertyChangedEventArgs ( propertyName ) );
    }
}

Then you use it like this (that's right Results is also s ViewModel):

public class Results : ViewModelBase
{
    private bool passed;

    public bool Passed
    {
        get { return passed; }
        set
        {
            passed = value;
            OnPropertyChanged ( );
        }
    }
}
Seb
  • 620
  • 1
  • 5
  • 11