0

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.

Community
  • 1
  • 1
xa19
  • 43
  • 1
  • 7
  • Where is your BooleanConverter defined? Can you upload that piece of code as well? – Stunna Sep 24 '15 at 20:46
  • Here's the code: http://pasted.co/6dd634db – xa19 Sep 24 '15 at 21:12
  • 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 Sep 24 '15 at 21:31
  • 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 Sep 24 '15 at 21:33
  • I have a sample that I responded to [here](http://stackoverflow.com/a/26664473/3109213) – Stunna Sep 24 '15 at 21:35
  • It looks like this: there are a few dimensions fields for the 3D object. By default, there are fields for the cuboid (radio button "cuboid" checked), like length, width, height. There is a second radio button "cylinder". What I want is to change the name "length" to "diameter" (and vice versa, when the first radio button is selected again), when the 'cylinder' is checked. – xa19 Sep 25 '15 at 05:50

1 Answers1

0

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.

xa19
  • 43
  • 1
  • 7