如何将按钮的DataContext更改为父级DataContext?
我在 WPF 中有 2 个类:
- 会面
- 与人
在会议中我有 2 个 ObservableCollections;包含 People
对象的 AttendingMeeting
和 NotAttendingMeeting
在 xaml 中,DataContext
设置为 Meeting
>,并且我有 2 个 DataGrid
(AttendingMeeting
和 NotAttendingMeeting
),
我需要为每个添加一个 Button
要在 Meeting
中添加和删除的 DataGrid
,但是我需要将 Button
上的 DataContext
更改为例如:将 AttendingMeeting
改为 Meeting
,以便 Meeting
类可以处理在 People
中添加和删除 People
>ObservableCollections。
我如何在 xaml 中执行此操作(将 DataContext
从 AttendingMeeting
项目更改为家长 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 DataGrid
s (AttendingMeeting
and NotAttendingMeeting
)
I need to add a Button
to each of the DataGrid
s 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 ObservableCollection
s.
How can I do this in xaml (changing the DataContext
from AttendingMeeting
items to the parents parent-> Meeting
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您尝试从
DataGrid
内绑定到 Meeting 类上的命令:您可以在绑定上使用
RelativeSource
来访问DataContext<您感兴趣的 /code> 。您的标记看起来与此类似:
您还可以使用
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 theDataContext
you're interested in. You're markup would look something similar to this:You could also use
ElementName
to bind to a parent that has theDataContext
you're interested in if you're dealing with lots of nesting and aRelativeSource
would be too complicated.