如何将按钮的DataContext更改为父级DataContext?

发布于 2024-08-05 05:27:57 字数 802 浏览 2 评论 0原文

我在 WPF 中有 2 个类:

  • 会面
  • 与人

在会议中我有 2 个 ObservableCollections;包含 People 对象的 AttendingMeetingNotAttendingMeeting

在 xaml 中,DataContext 设置为 Meeting >,并且我有 2 个 DataGridAttendingMeetingNotAttendingMeeting),

我需要为每个添加一个 Button要在 Meeting 中添加和删除的 DataGrid,但是我需要将 Button 上的 DataContext 更改为例如:将 AttendingMeeting 改为 Meeting,以便 Meeting 类可以处理在 People 中添加和删除 People >ObservableCollections。

我如何在 xaml 中执行此操作(将 DataContextAttendingMeeting 项目更改为家长 parent-> Meeting)?

I have 2 Classes in WPF:

  • Meeting
  • People

In meeting I have 2 ObservableCollections; AttendingMeeting and NotAttendingMeeting that contain People objects

In the xaml the DataContext are set to Meeting, and I have 2 DataGrids (AttendingMeeting and NotAttendingMeeting)

I need to add a Button to each of the DataGrids to add and remove from Meeting, But then I need to change the DataContext on the Button from e.g.: AttendingMeeting to Meeting, so that the Meeting class can handle the adding and removing People from the ObservableCollections.

How can I do this in xaml (changing the DataContext from AttendingMeeting items to the parents parent-> Meeting)?

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

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

发布评论

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

评论(1

习惯成性 2024-08-12 05:27:57

假设您尝试从 DataGrid 内绑定到 Meeting 类上的命令:

您可以在绑定上使用 RelativeSource 来访问 DataContext<您感兴趣的 /code> 。您的标记看起来与此类似:

<Grid DataContext="..."> <!-- Assuming a grid is you're layout root and that it's datacontext is the Meeting you spoke of -->
   ...
   <DataGrid ...>
      ...

      <Button Content="Click Me!"
              Command="{Binding Path="DataContext.RemovePersonCommand"
                                RelativeSource={RelativeSource FindAncestor,
                                                AncestorType={x:Type Grid}}}"
              CommandParameter="{Binding}"/> <!-- This would be binding to the current person -->
      ...
   </DataGrid>
   ...
</Grid>

您还可以使用 ElementName 绑定到具有您的 DataContext 的父级。如果您正在处理大量嵌套,并且 RelativeSource 会太复杂,那么我们很感兴趣。

Assuming you're trying to bind to a Command on the Meeting class from within the DataGrid:

You can use a RelativeSource on your binding to get to the DataContext you're interested in. You're markup would look something similar to this:

<Grid DataContext="..."> <!-- Assuming a grid is you're layout root and that it's datacontext is the Meeting you spoke of -->
   ...
   <DataGrid ...>
      ...

      <Button Content="Click Me!"
              Command="{Binding Path="DataContext.RemovePersonCommand"
                                RelativeSource={RelativeSource FindAncestor,
                                                AncestorType={x:Type Grid}}}"
              CommandParameter="{Binding}"/> <!-- This would be binding to the current person -->
      ...
   </DataGrid>
   ...
</Grid>

You could also use ElementName to bind to a parent that has the DataContext you're interested in if you're dealing with lots of nesting and a RelativeSource would be too complicated.

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