Headers / StaysOpenOnClick with binding to ItemsSource in ContextMenu
-
Mittwoch, 1. August 2012 21:58
I've been struggling to figure out how to bind a dynamic collection of menu items for a ContextMenu, while having the ability to still access things such as the Header and StaysOpenOnClick properties available in a standard MenuItem.
For instance, I have the following ContextMenu:
<Label Style="{StaticResource TextBlockValidationStyle}" Content="{Binding Path=ColumnMapping.DestinationColumnDataType, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource validationTemplate}" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DataContext}"> <Label.ContextMenu> <ContextMenu StaysOpen="True" ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.Tag.DestinationColumnDataTypeValidations}"> <ContextMenu.ItemTemplate> <DataTemplate> Some stuff to bind to </DataTemplate> </ContextMenu.ItemTemplate> </ContextMenu>
Is there anyway to access those standard properties? Keeping the ContextMenu open is of the utmost importance, but the other things, like headers/icons, would be really nice as well .
Alle Antworten
-
Donnerstag, 2. August 2012 00:25Moderator
This is actually easier than you think...
MainWindow.xaml
<Window x:Class="ContextMenuInCode.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="MenuItem"> <Setter Property="Header" Value="{Binding Header}"/> <Setter Property="StaysOpenOnClick" Value="{Binding StaysOpenOnClick}"/> </Style> </Window.Resources> <Grid> <Border Background="AliceBlue" > <Border.ContextMenu> <ContextMenu ItemsSource="{Binding MyMenuItems}" /> </Border.ContextMenu> </Border> </Grid> </Window>MainWindow.xaml.cs
using System.Collections.Generic; using System.Windows; using System.Windows.Input; using System.Collections.ObjectModel; namespace ContextMenuInCode { public partial class MainWindow : Window { public ObservableCollection<MyMenuItem> MyMenuItems { get; set; } public MainWindow() { InitializeComponent(); MyMenuItems = new ObservableCollection<MyMenuItem> { new MyMenuItem { Header="One", StaysOpenOnClick=true}, new MyMenuItem { Header="Two", StaysOpenOnClick=true}, }; DataContext = this; } } public class MyMenuItem { public string Header { get; set; } public bool StaysOpenOnClick { get; set; } public ICommand Command { get; set; } } }You can add anything you need to your MyMenuItem class, like (ICommand)Command, (object)CommandParameter, etc.
Now you see the concept, I'm sure you'll find plenty more examples.
[EDIT] Updated to ObservableCollection, as you said "dynamic" as in add/remove. If you want to make the properties dynamic, you need to implement INotifyPropertyChanged on the MyMenuItem class.
Regards,
Pete
#PEJL
- Bearbeitet XAML guyMicrosoft Community Contributor, Moderator Donnerstag, 2. August 2012 00:45
- Als Antwort markiert NGAL Donnerstag, 2. August 2012 14:16
-
Donnerstag, 2. August 2012 14:18I forgot I could set the MenuItem styles of the collection! Thanks for the reminder. Since I'm using MVVM, the other stuff wasn't necessary for my purposes. Thanks again.

