场景:我有一个ListBox,ListBoxItems有一个DataTemplate。我想要做的是在DataTemplate中放置一个ContextMenu。问题是我希望这个ContextMenu ItemsSource根据窗口中的某些属性而不同。我最初的想法是,我可以将ItemsSource绑定到窗口中的Property,并返回ItemsSource;但是,我似乎无法正确绑定此属性。我相信这是因为我在DataTemplate中,因此DataContext(我相信这是正确的词)是ListBoxItem而不是窗口。 我怎样才能将DataTemplate中的ContextMenu绑定到DataTemplate之外的Property。
答案 0 :(得分:5)
您可以使用RelativeSource FindAncestor语法
从窗口获取DataContext<DataTemplate>
<TextBlock Text="{Binding MyInfo}">
<TextBlock.ContextMenu>
<Menu ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyContextMenuItems}"/>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
不完全确定,但绑定是正确的...... 如果您的DataContext是另一种对象类型,则只需更改AncestorType(例如,通过UserControl)。
答案 1 :(得分:2)
这可能是AttachedProperty的合适人选。基本上你要做的是将ContextMenu包装在UserControl中,然后将依赖属性添加到UserControl。例如:
<UserControl x:Class="MyContextMenu" ...>
<UserControl.Template>
<ContextMenu ItemSource="{Binding}" />
</UserControl.Template>
</UserControl>
public static readonly DependencyProperty MenuItemsSourceProperty = DependencyProperty.RegisterAttached(
"MenuItemsSource",
typeof(Object),
typeof(MyContextMenu),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetMenuItemsSource(UIElement element, Boolean value)
{
element.SetValue(MenuItemsSourceProperty, value);
// assuming you want to change the context menu when the mouse is over an element.
// use can use other events. ie right mouse button down if its a right click menu.
// you may see a perf hit as your changing the datacontext on every mousenter.
element.MouseEnter += (s, e) => {
// find your ContextMenu and set the DataContext to value
var window = element.GetRoot();
var menu = window.GetVisuals().OfType<MyContextMenu>().FirstOrDefault();
if (menu != null)
menu.DataContext = value;
}
}
public static Object GetMenuItemsSource(UIElement element)
{
return element.GetValue(MenuItemsSourceProperty);
}
<Window ...>
<Window.Resources>
<DataTemplate TargetType="ListViewItem">
<Border MyContextMenu.MenuItemsSource="{Binding Orders}">
<!-- Others -->
<Border>
</DataTemplate>
</Window.Resources>
<local:MyContextMenu />
<Button MyContextMenu.MenuItemsSource="{StaticResource buttonItems}" />
<ListView ... />
</Window>
public static IEnumerable<DependencyObject> GetVisuals(this DependencyObject root)
{
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(root, i);
yield return child;
foreach (var descendants in child.GetVisuals())
{
yield return descendants;
}
}
}
public static DependencyObject GetRoot(this DependencyObject child)
{
var parent = VisualTreeHelper.GetParent(child)
if (parent == null)
return child;
return parent.GetRoot();
}
此示例未经测试我今晚稍后会看一看并确保其准确无误。