Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelayCommand called in "old" DataContext

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.

like image 535
Kai Avatar asked Nov 06 '22 03:11

Kai


1 Answers

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.

like image 181
Kai Avatar answered Nov 09 '22 16:11

Kai