0
votes

I am trying to relate the label text to the radio buttons value e.g. if radio is checked, then the label text is "x". If not, it's "y". In my XML:

<RadioButton x:Name="radio1" Content="Option1" GroupName="Group1" IsChecked="{Binding BoolValue, Converter={StaticResource BooleanConverter}, ConverterParameter='true', Mode=TwoWay}" />
<RadioButton Content="Option2" GroupName="Group2" IsChecked="{Binding BoolValue, Converter={StaticResource BooleanConverter}, ConverterParameter='false', Mode=TwoWay}"/>
...
...
<Label Content="{Binding LabelText, UpdateSourceTrigger=PropertyChanged}" Width="70"/>

In the code: (The _shape is bind to the radio button IsChecked;)

private bool _boolValue;
public bool BoolValue
{
    get { return _boolValue; }
    set
    {
        _boolValue= value;
        PackLengthLabel = (_boolValue == true)? "x" : "y";
        OnPropertyChanged("BoolValue");
    }
}

And the label text property:

private string _labelText;
public string LabelText
{
    get { return _labelText; }
    set
    {
        _labelText = value;
        OnPropertyChanged("LabelText");
    }
}

The problem is that the changes don't affect the label text - it is the same all the time, no matter which checkbox is checked. The boolean value and the text value are changing (checked in the setters). I've also checked if the label is trying to get the _labelText from the getter but it doesn't. I also tried different binding modes, but the text was all the same. The only way it affects the other controls is by binding directly to the other properties e.g.:

IsEnabled="{Binding IsChecked, ElementName=radio1}" 

Edit1:

I can get it working in two ways:

  1. setting the label content value in the View code behind, refering to the elements properties

  2. using the code here: https://stackoverflow.com/a/23642108/3974198

But I'm still curious, why the simple getter and setter of the label text value didn't do the job.

1
Where is your BooleanConverter defined? Can you upload that piece of code as well?Stunna
Here's the code: pasted.co/6dd634dbxa19
So I'm struggling a little with the purpose of the implementation. If I understand this correctly, you want to have "x" always checked, but it could be radio button 1, or radio button 2. But I think what might help you is to do a bit of separation of concerns. Is the "X", "Y" your actual data values you want to store, or are you indeed trying to keep a boolean value stored as your data type?Stunna
I guess what I'm getting to, is if trying to use MVVM, IValueConverters are really meant for a "visual" representation, since they usually sit on your view. You seem to actually want to keep the X, Y, logged in your data/Model?Stunna
I have a sample that I responded to hereStunna

1 Answers

0
votes

Finally I got it. I had the property setters and getters in the View and the ViewModel. The problem was that the View had inherited from INotifyPropertyChanged, but the ViewModel didnt THOUGH in the ViewModel I could use the OnPropertyChanged, without getting any errors.