RelayCommand called in "old" DataContext

638 views Asked by At

I am using RelayCommands in my ViewModels to ged rid of the code behinds. The RelayCommands are working, except in this situation: the ViewModel in which the RelayCommands are defined is changed with the change of the datacontext. If a command is being fired after the datacontext change, it is fired in the "old" viewmodel. While debugging I can see that the properties are from the old datacontext. But in the view is everything fine because the correct data of the correct viewmodel is displayed.

So is it possible that the bounded commands are not able to react to a change of the datacontext? Or might there something else being wrong?

Here a snip of the code:

<TabControl x:Name="TestView" DataContext="{Binding Path=SelectedParentElement}" 
                TabStripPlacement="Top" ItemsSource="{Binding Path=ChildElements, Mode=OneWay}"
                SelectedValue="{Binding Path=SelectedChildElement, Mode=TwoWay}">
<TabControl.ContextMenu>
                    <ContextMenu>
 <MenuItem Header="Klassifikation" Name="Klassifikation">
                                <MenuItem Header="Kindebene" Name="KlassisfizierendNext" Command="{Binding KlassifizierendNextCommand}"/>
                            </MenuItem>
                    </ContextMenu>
                </TabControl.ContextMenu>
.......
            </TabControl>

The datacontext of the tabcontrol is correctly changing, but after changing the command is being called in the old datacontext.

2

There are 2 answers

0
Kai On BEST ANSWER

Found a solution: http://social.msdn.microsoft.com/Forums/en/wpf/thread/2cbec263-df05-4000-9077-35861fc2fa8e

But it seems to be a bug that the datacontext auf contextmenus dont change right.

0
micahtan On

@Rick. The reason for the downvote:

  1. It doesn't answer the OP's question. He's debugging the data context, you're doing everything in XAML.

  2. It doesn't work for me. I assume that this is due to the same bug that the OP noticed.

To be fair, this isn't your fault: it's clearly a bug in the way that Microsoft is handling DataContext inheritance for ContextMenus, but your answer doesn't appear to have solved the OP's problem, nor mine.

enter image description here