由于 ContextMenu 不是 DataGrid 可视化树的一部分,因此我们无法访问 DataGrid 的 DataContext 中定义的属性。但是我们可以做下一个解决方法来做到这一点。
- 创建一个布尔附加属性来定义下一件事;如果添加的属性的值为true,它将找到该属性所附加到的对象的可视父级,并将父级的DataContext分配给该属性所附加到的对象的Tag属性,反之,如果值为附加属性为 false ,标签属性将被赋值为 null。
- 创建一个扩展帮助方法,它将扫描调用者的可视化树并返回它的(调用者)特定类型的父级。
- 为 DataGridCell 对象创建默认样式,该样式将使用上面描述的依赖属性并将定义 ContextMenu。将此样式设置为 App.xaml 中的资源(考虑到此样式将被项目中的所有 DataGridCell 对象使用)。
样式代码(应该在 App.xaml 中)
<Style TargetType="DataGridCell">
<Setter Property="dataGridCreateColumnAndBindIteFromCodeBehind:DataGridAttached.SetDataGridDataContextToTag" Value="True"></Setter>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="MenuItem One"
Command="{Binding CmdMenuItemOne}" />
<MenuItem Header="MenuItem Two"
Command="{Binding CmdMenuItemTwo}" />
</ContextMenu>
</Setter.Value>
</Setter></Style>
附属性代码
public class DataGridAttached
{
public static readonly DependencyProperty SetDataGridDataContextToTagProperty = DependencyProperty.RegisterAttached(
"SetDataGridDataContextToTag", typeof (bool), typeof (DataGridAttached), new PropertyMetadata(default(bool), SetParentDataContextToTagPropertyChangedCallback));
public static void SetSetDataGridDataContextToTag(DependencyObject element, bool value)
{
element.SetValue(SetDataGridDataContextToTagProperty, value);
}
public static bool GetSetDataGridDataContextToTag(DependencyObject element)
{
return (bool) element.GetValue(SetDataGridDataContextToTagProperty);
}
private static void SetParentDataContextToTagPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
PerformDataContextToTagAssignment(dependencyObject as FrameworkElement, (bool)args.NewValue);
}
private static void PerformDataContextToTagAssignment(FrameworkElement sender, bool isAttaching)
{
var control = sender;
if (control == null) return;
if (isAttaching == false)
{
control.Tag = null;
}
else
{
var dataGrid = control.FindParent<DataGrid>();
if (dataGrid == null) return;
control.Tag = dataGrid.DataContext;
}
}
}
帮助代码
public static class VisualTreeHelperExtensions
{
public static T FindParent<T>(this DependencyObject child) where T : DependencyObject
{
while (true)
{
//get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
return parent;
child = parentObject;
}
}
}
问候。