new*_*man 15 wpf datagrid relationship
在DataGrid的Command的事件处理程序中,我在ExecutedRoutedEventArgs中获取DataGridCell.但是,我无法弄清楚如何获取其关联的DataGrid和DataGridRow.非常感谢您的帮助.
Jer*_*all 15
你可能想设置某种RelativeSource
绑定,可以通过a获得"父网格/行" {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}
,但你的问题让我思考......
你可以:
使用反射:
var gridCell = ....;
var parentRow = gridCell
.GetType()
.GetProperty("RowOwner",
BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(null) as DataGridRow;
Run Code Online (Sandbox Code Playgroud)
使用VisualTreeHelper
:
var gridCell = ...;
var parent = VisualTreeHelper.GetParent(gridCell);
while(parent != null && parent.GetType() != typeof(DataGridRow))
{
parent = VisualTreeHelper.GetParent(parent);
}
Run Code Online (Sandbox Code Playgroud)
这是我认为的完整答案......
private void Copy(object sender, ExecutedRoutedEventArgs e)
{
DataGrid grid = GetParent<DataGrid>(e.OriginalSource as DependencyObject);
DataGridRow row = GetParent<DataGridRow>(e.OriginalSource as DependencyObject);
}
private T GetParent<T>(DependencyObject d) where T:class
{
while (d != null && !(d is T))
{
d = VisualTreeHelper.GetParent(d);
}
return d as T;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11152 次 |
最近记录: |