在 Silverlight 中,子控件视图模型如何访问父数据上下文?

发布于 2024-11-29 07:18:24 字数 351 浏览 7 评论 0原文

背景:
我有类似于以下伪代码的 xaml

...
<DataContext><vm:parentVM/></DataContext>
<Grid x:Name="LayoutRoot">
...
<local:myControl Grid.Column="0" Grid.Row="1" DataContext="{Binding vmChild}" />

问题:
当我在视图模型 vmChild 的上下文中处理一些代码时,如何访问视图模型 vmParent 中的属性和方法

BACKGROUND:
I have xaml similar to the following pseudo code

...
<DataContext><vm:parentVM/></DataContext>
<Grid x:Name="LayoutRoot">
...
<local:myControl Grid.Column="0" Grid.Row="1" DataContext="{Binding vmChild}" />

QUESTION:
When I am processing some code in the context of the view model vmChild, how do I access properties and methods in the view model vmParent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-12-06 07:18:24

试试这个:假设列表框的数据上下文位于您的子虚拟机中,而父虚拟机是一个网格,其数据上下文是从父虚拟机设置的。一些施展诡计可以让你获得货物。

  (((sender as ListBox).Parent as Grid).DataContext as ParentVM).VMProperty = "Cool";

Try this: Assuming the Listbox's data context is in your childVM and the parent is a Grid which has its data context set from the ParentVM. Some casting trickery can allow you to access the goods.

  (((sender as ListBox).Parent as Grid).DataContext as ParentVM).VMProperty = "Cool";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文