0

For Example: We need to dynamically bind a RadioButton Value property with two different properties of ViewModel.

View Model

public class MyViewModel
{

//Property-1 to bind with RadioButton
        public bool Accepted
        {
            get;
            set;
        }

//Property-2 to bind with RadioButton
        public bool Enable
        {
            get;
            set;
        }

//Property to Identify which property should bind with radio button.
        public bool Mode
        {
            get;
            set;           
        }
}

Xaml

<RadioButton Value="{Binding Accepted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Is it possible to dynamically bind Accepted or Enable property according to the Mode property?

  • One solution came up is using IMultiValueConverter and MultiBinding. Is it a proper method?
1
  • 1
    Do you have control over the viewmodel? why not bind to a new property who's value reflects the value of Accepted or Enable based on the value of Mode? Commented Nov 18, 2015 at 6:36

1 Answer 1

3

You cannot change the binding from the view model. You could however create a new property which you bind to that then delegates its value to the correct other value, e.g.:

public class ViewModel
{
    public bool Prop1 { get; set; }
    public bool Prop2 { get; set; }
    public bool Use2 { get; set; }

    public bool Prop
    {
        get { return Use2 ? Prop2 : Prop1; }
        set
        {
            if (Use2)
                Prop2 = value;
            else
                Prop1 = value;
        }
    }
}

Of course this example is missing the INotifyPropertyChanged implementation details.

Sign up to request clarification or add additional context in comments.

2 Comments

We need to NotifyPropertyChanged for new property while each property's changes. RIght ?
Yeah, the event needs to be there for every property which changes you want to affect the view. So in this case, you could only implement it for Prop but when you change any of the other tree properties, you need to remember to also raise the event for Prop since that’s what will indirectly change and then update the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.