Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <DataTemplate>
- <TextBlock Text="{Binding MyInfo}">
- <TextBlock.ContextMenu>
- <Menu ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyContextMenuItems}"/>
- </TextBlock.ContextMenu>
- </TextBlock>
- </DataTemplate>
- <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();
- }
Add Comment
Please, Sign In to add comment