WPF:访问同一控件中的两个 DataContext

发布于 2024-08-13 03:06:09 字数 1743 浏览 3 评论 0原文

我正在使用 MVVM 方法,并且我的 ViewModel 中有一个名为 DatabasesSubFrame 的对象,它通过 DataTemplate 来显示 ListBox。我想在 ListBox 下方显示一个 Button,它绑定到当前 SelectedItemDatabasesSubFrame 上的属性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 DataTemplated 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 DataTemplated.

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 技术交流群。

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

发布评论

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

评论(1

撕心裂肺的伤痛 2024-08-20 03:06:09

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:

{Binding ElementName=Root, Path=DataContext.MyViewModelsProperty}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文