0

I am trying to understand if it's possible to change the property the control is binding using triggers...

So i have two Properties for example in ViewModel and using a trigger depending if the first property is null, the control will binding the first property if isn't null, or binding the second one.

This is possible to achieve? Will work with Commands too?

Thanks in advance!

4 Answers 4

1

I typically would not recommend trying this. A different, IMO cleaner, approach is to use the trigger to change the visibility of the object in question.

This allows you to collapse the control and make a different one, with the appropriate binding setup, visible based on a pair of triggers. The advantage here is that you're not having to refresh the bindings continually, plus, your interface has a 1-1 correlation with the properties in your ViewModel.

This will also work with Commands, as your commanding object (ie: Button) can just be switched out to the newly appropriate one.

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

Comments

1

Maybe you can use multibinding with a ValueConverter:

<MultiBinding Converter="{StaticResource MyConverter}"> <Binding Path="FirstProperty" /> <Binding Path="SecondProperty" /> </MultiBinding> 

MyConverter should evaluate and return the right property:

public class MyConverter: IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values[0] ?? values[1]; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

Comments

0

It would be simpler to have a single property in your ViewModel that provides the value of the first property if it is not null, else the value of the second property. Something like:

public String BindToMe { get { return FirstProperty ?? SecondProperty; } } 

Would this work for you?

1 Comment

Note that this would require raising PropertyChanged for BindToMe whenever FirstProperty and SecondProperty change in the VM.
0

Why can't PriorityBinding satisfy this requirement?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.