Dishant
Dishant

Reputation: 1595

How to execute Converter when value of the another control get's changed?

I have two ComboBox ComboBox1 and ComboBox2, I want to execute ComboBox2 converter whenever ComboBox1 SelectedItem gets changed, How it can be done in XAML.

So far I have this XAML for ComboBox2:

ItemsSource="{Binding MyItems, Converter={StaticResource MYConverter}, ConverterParameter= {Binding ElementName=comboBox1, Path=SelectedItem,Mode=TwoWay}}" 

Upvotes: 1

Views: 111

Answers (2)

Neto Costa
Neto Costa

Reputation: 252

On the code-behind, you could create something like this:

comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1SelectionChanged);

It will generate an event handler that wiil be called by delegate, the handler should look like this:

void comboBox1SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //Your logic here
    }

Don’t forget:

u

sing System.Windows;
using System.Windows.Controls;

Upvotes: 0

Muhammad Azeez
Muhammad Azeez

Reputation: 936

A binding is re-evaluated only when the value of the binding is changed, not the converter parameter, so suppose:

  1. SelectedItem1 property of the ViewModel is bound to ComboBox1sSelectedItem property.

  2. SelectedItem2 property of the ViewModel is bound to ComboBox2s SelectedItem property.

  3. ComboBox2s ItemSource property of the ViewModel is bound to a property on your ViewModel called MyItems:

Whenever SelectedItem1 is changed, you should raise PropertyChanged event for MyItems. This way, the binding is re-evaluated and the converter is executed.

P.S: Please give more context about your questions in the future, e.g. what your ViewModel looks like.

Upvotes: 2

Related Questions