我在 WPF 中有 2 个课程:
在会议中我有 2ObservableCollections
; AttendingMeeting
and NotAttendingMeeting
包含People
objects
在 xaml 中DataContext
被设置为Meeting
,我有 2DataGrid
s (AttendingMeeting
and NotAttendingMeeting
)
我需要添加一个Button
到每一个DataGrid
s 添加和删除Meeting
,但是我需要改变DataContext
on the Button
来自例如:AttendingMeeting
to Meeting
, 所以这样Meeting
类可以处理添加和删除People
来自ObservableCollection
s.
我怎样才能在xaml中做到这一点(改变DataContext
from AttendingMeeting
给父母的物品parent-> Meeting
)?
假设您尝试从以下内容绑定到 Meeting 类上的命令DataGrid
:
您可以使用RelativeSource
在您的绑定上到达DataContext
您感兴趣的。您的标记将类似于以下内容:
<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
太复杂了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)