Question

I have a ComboBox that looks like this:

 <ComboBox SelectedValue="{Binding Mode}" SelectedValuePath="Key" ItemsSource="{Binding MyModes}" DisplayMemberPath="Value" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding DataContext.SetModeCommand,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}"  IsEnabled="{Binding IsForSet}" CommandParameter="{Binding}" ></i:InvokeCommandAction>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ComboBox>

SelectedValue and ItemsSource are bind to variables in the model.

And the command: SetModeCommand sitting in ViewModel.

I want that command will not work when changing the variable in the model, but will only work when changing it through the UI.

So I created a variable called IsForSet (bool) and when I change the variable Mode through the model I put it false :

IsForSetUM = false;
Mode = 202;
IsForSetUM = true;

My problem is that the command is not called as soon as I change the Mode. But only after I change the IsForSetUM to true.

I have a solution to this!!

My question is why the command is not called immediately when I change the Mode?

Was it helpful?

Solution

The solution is to set NotifyOnSourceUpdated on your Binding to true and use the SourceUpdated event in your EventTrigger like so:

<ComboBox SelectedValue="{Binding Mode, NotifyOnSourceUpdated=True}" SelectedValuePath="Key" ItemsSource="{Binding MyModes}" DisplayMemberPath="Value" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SourceUpdated">
                            <i:InvokeCommandAction Command="{Binding DataContext.SetModeCommand,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}" CommandParameter="{Binding}" ></i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top