在“旧”中调用 RelayCommand数据上下文
我在 ViewModel 中使用 RelayCommands 来消除隐藏的代码。 RelayCommands 正在工作,但在这种情况下除外:定义 RelayCommands 的 ViewModel 随着数据上下文的更改而更改。如果在数据上下文更改后触发命令,则会在“旧”视图模型中触发该命令。在调试时,我可以看到这些属性来自旧的数据上下文。但在视图中一切都很好,因为显示了正确视图模型的正确数据。
那么有界命令是否有可能无法对数据上下文的更改做出反应? 或者可能还有其他问题吗?
这里是代码片段:
<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>
选项卡控件的数据上下文正确更改,但更改后在旧数据上下文中调用命令。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了解决方案: http://social .msdn.microsoft.com/Forums/en/wpf/thread/2cbec263-df05-4000-9077-35861fc2fa8e
但这似乎是一个错误,datacontext auf contextmenus 没有正确更改。
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.
@里克。否决的原因:
它没有回答OP的问题。他正在调试数据上下文,而您正在 XAML 中执行所有操作。
这对我不起作用。我认为这是由于 OP 注意到的相同错误造成的。
公平地说,这不是你的错:这显然是 Microsoft 处理 ContextMenus 的 DataContext 继承方式中的一个错误,但你的答案似乎没有解决 OP 的问题,也没有解决我的问题。
@Rick. The reason for the downvote:
It doesn't answer the OP's question. He's debugging the data context, you're doing everything in XAML.
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.