WPF:访问同一控件中的两个 DataContext
我正在使用 MVVM 方法,并且我的 ViewModel 中有一个名为 DatabasesSubFrame
的对象,它通过 DataTemplate
来显示 ListBox
。我想在 ListBox
下方显示一个 Button
,它绑定到当前 SelectedItem
和 DatabasesSubFrame
上的属性code> 正在使用 DataTemplate
构建的对象。
我知道如何通过在与 ListBox
共享的祖先上设置 DataContext
并使用 {Binding /}
来引用当前选定的项目。在此示例中,共享祖先是 StackPanel。如果没有显式设置 DataContext
,我只需执行 {Binding SomeProperty}
即可轻松绑定到 DatabasesSubFrame
对象上的属性。但是,如果我在显式设置的 DataContext
中执行 {Binding SomeProperty}
,它会引用错误的 DataContext
。
如何在这里访问“原始”DataContext
?我尝试搞乱RelativeSources和TemplatedParents,但不知道如何将它们放入其中。
<DataTemplate DataType="{x:Type VM:DatabasesSubFrame}">
<StackPanel DataContext="{Binding Databases}" >
<ListBox Name="DbInfoBox"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding ShortName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- Problem: The Command and V:CreateCommandBinding.Command are set incorrectly here. How do I access OpenDbCommand from the top-level DataTemplate's DataContext? -->
<Button Content="Open Database"
CommandParameter="{Binding /}"
Command="{Binding ???, Path=OpenDbCommand.Command}"
V:CreateCommandBinding.Command="{Binding ???, Path=DataContext.OpenDbCommand}"/>
</StackPanel>
</DataTemplate>
I am using an MVVM approach, and I have an object from my ViewModel called DatabasesSubFrame
which is DataTemplate
d to show a ListBox
. I want to display a Button
below the ListBox
, which binds to both the currently SelectedItem
, and a property on the DatabasesSubFrame
object which is being DataTemplate
d.
I know how to refer to the currently selected item, by setting the DataContext
on a shared ancestor with the ListBox
and use {Binding /}
. In this example the shared ancestor is a StackPanel
. And if the DataContext
wasn't explicitly set there I could easily bind to a property on the DatabasesSubFrame
object by just doing {Binding SomeProperty}
. However, if I do {Binding SomeProperty}
within the explicitly set DataContext
, it refers to the wrong DataContext
.
How do I access the "original" DataContext
here? I tried messing with RelativeSources and TemplatedParents but couldn't figure out how to fit them in.
<DataTemplate DataType="{x:Type VM:DatabasesSubFrame}">
<StackPanel DataContext="{Binding Databases}" >
<ListBox Name="DbInfoBox"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding ShortName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- Problem: The Command and V:CreateCommandBinding.Command are set incorrectly here. How do I access OpenDbCommand from the top-level DataTemplate's DataContext? -->
<Button Content="Open Database"
CommandParameter="{Binding /}"
Command="{Binding ???, Path=OpenDbCommand.Command}"
V:CreateCommandBinding.Command="{Binding ???, Path=DataContext.OpenDbCommand}"/>
</StackPanel>
</DataTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这个问题将帮助您找到您的答案。另一个技巧是将窗口名称设置为“Root”之类的名称。然后,您可以使用以下方法获取窗口的原始数据上下文:
I think this question will help you to find the answer to yours. Another trick is to set the Name of the Window to something like "Root". You can then get at the window's original datacontext by using: