I am trying to add an item to a WPF-ContextMenu that is only used to "describe" the items below but I am not sure how to add a simple line of text above all the items, where the text is centrally aligned and the text is not selectable like the normal MenuItems.
I tried something like this:
<ContextMenu Grid.Row="0" StaysOpen="False">
<TextBlock Text="Add New:" IsEnabled="False" HorizontalAlignment="Center"/>
<MenuItem Header="one"/>
<MenuItem Header="two (horizontal)"/>
<MenuItem Header="two (vertical)"/>
<MenuItem Header="three"/>
<MenuItem Header="four"/>
<MenuItem Header="six"/>
</ContextMenu>
but unfortunately the TextBlock is neither aligned centrally, nor is it unselectable.
The problem is that using a MenuItem and setting the IsEnabled-property to false, the text is not normal black anymore and also I can't really align it centrally.
Hopefully, someone can think of a easy solution here, I simply couldn't find anything.
I will suggest you to create the SubMenu items for menuitems. In this way menuitems will group all the submenuitems under it.
I had given the answer to create the context menu with menuitem and submenuitems purely using mvvm. Here you can refer to it.
WPF Context Menu with dropdown list showing hyperlinks
OR For the case you mentioned. I tried this:
<ContextMenu StaysOpen="False">
<MenuItem Header="Add New:" IsEnabled="False" HorizontalAlignment="Center">
<MenuItem.Style>
<Style TargetType="{x:Type MenuItem}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
<MenuItem Header="one"/>
<MenuItem Header="two (horizontal)"/>
<MenuItem Header="two (vertical)"/>
<MenuItem Header="three"/>
<MenuItem Header="four"/>
<MenuItem Header="six"/>
</ContextMenu>
and I got menu like
Add New: is non-selectable and also mouse over does not highlight it.
Hope it helps. Thanks
Related
I have a context menu, and it had a few items.
Like always if scroll over one of the items I can see the sub-items in that menu.
But its size is very huge I can't able to see the full lists or scroll it.
Since the sub-items are loaded dynamically I use like :
<ContextMenu >
<MenuItem Header="Mobiles" ItemSource={Binding...}>
<MenuItem.ItemContainerStyle>
<Stryle targeyType="MenuItem">
<Setter property="HeaderTemplate">
<Setter.Value>
<DataTemplate> <TextBlock Text ="{Binding MobilName}"/> </DataTemplate>
</Setter.Value>
<Setter Property="Command" Value="{Binding ViewMobileCommand}"/>
</Setter >
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</ContextMenu >
Can anyone suggest a better way to tackle this?
I have a menu with the two following menu items:
<MenuItem Header="Item1">
<MenuItem.Icon>
<cc:Icon ImageSource="..." Size="22"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Item2">
<MenuItem.Icon>
<cc:Icon ImageSource="..." Size="22"/>
</MenuItem.Icon>
</MenuItem>
As you can see each Item has a name as the "Header" and an Icon (my own control) as an an "Icon".
Instead of writing them explicitly in the XAML I want to Bind the ItemsSource to a List<MenuItem> object in my ViewModel, like this:
ItemsSource="{Binding Path=SomeMenu}"
My MenuItem class has the fields "String Name" and ImageSource Icon which I want to bind to the Header and the Icon parts of my menu item exactly how I did here.
I thought the right way was to use a DataTemplate, like this:
<Menu.ItemTemplate>
<DataTemplate>
<!-- TODO: Bind the Header and Icon properties-->
<TextBlock Text="{Binding Path=Name}"/>
<!-- how to bind the icon? -->
</DataTemplate>
<Menu.ItemTemplate>
But I don't understand how to define the Header and the Icon templates separately.
How do I accomplish this, than?
update: I want the <cc:Icon> to appear whereever in the menuItem template there is a <contentPresenter Source="Icon"> and the textBox the appear whereever there is a <contentPresenter Source="Header">
i believe this is what you want:
<Menu ItemsSource="{Binding SomeMenu}">
<Menu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Icon">
<Setter.Value>
<cc:Icon Source="{Binding Icon}"></Image>
</Setter.Value>
</Setter>
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</Menu.ItemContainerStyle>
</Menu>
or did i fail miserably again? :)
Basically, I want to show a context menu when the user right clicks a valid item on my data grid, however, all the answers I've found are made to do this with a data grid view, which I can't use since I am working on a WPF project. To be clear, I know how to deal with the context menu when right clicking, however, I need the menu only to popup when the user right clicks a row in the data grid. Please help me c:
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<Menu>
<MenuItem Header="Cut"/>
</Menu>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
You can try this to create ContextMenu.
<DataGrid Name="dGrid">
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Header="Click Here" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Have a code like:
<MenuItem ItemSource="SOURCE">
..sub MenuItems
</MenuItem>
How to create the right template that allows to bind the each subitem to the save command.
Something like that:
<MenuItem ItemsSource="{Binding SubItems}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding SaveCommand}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
This is how I did it in a project:
In the Window definition i "define" the commands workspace.
<Window x:Class="WorkForce.Views.MainWindow"
...
xmlns:commands="clr-namespace:WorkForce.Commands"
...
>
After that I connect them to each menu-item.
<MenuItem Header="_File">
<MenuItem Header="_New..." Command="commands:MainWindowCommands.NewFile"/>
<MenuItem Header="_Open..." Command="commands:MainWindowCommands.OpenFile"/>
<MenuItem Header="_Save..." Command="commands:MainWindowCommands.SaveFile"/>
</MenuItem>
I hope that is enough
I saw that you want to add it dynamically. Please look at this: WPF: How can you add a new menuitem to a menu at runtime?
I have a vertical menu set to the left side of the window. Its items open just on (above) it and this prevents the user from having a full view of the menu when an item is open.
I want each element to open just on the right of the menu, so as to have an entire view of both the rest of menu and the opened elements. How can this be done? May be with the aid of transforms or triggers?
Here is some code:
<MenuItem Header="Maths">
<MenuItem Background="LightGray" Header="Add"/>
<MenuItem Background="LightGray" Header="Subtract"/>
<MenuItem Background="LightGray" Header="Multiply"/>
<MenuItem Background="LightGray" Header="Divide"/>
</MenuItem>
So just to be clear, the MenuItem 'Maths' above is in a WPF Menu and you have changed that Menu's ItemsPanel to be a Vertical StackPanel or something so 'Maths' is above/below other sibling menu items. If so what is happening is that the default template for MenuItem's whose role is TopLevelHeader (MenuItem that has child Items and is directly within a Menu) is such that the popup is below (or above) the menu item. You will probably want to retemplate those menu items. On hacky (and ugly alternative) is to use the template that would be used for SubmenuHeader role menu items (i.e. a MenuItem that has child Items and is within another MenuItem). e.g.
<Menu HorizontalAlignment="Left">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<Menu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Style.Triggers>
<Trigger Property="Role" Value="TopLevelHeader">
<Setter Property="Template" Value="{DynamicResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}" />
</Trigger>
<Trigger Property="Role" Value="TopLevelItem">
<Setter Property="Template" Value="{DynamicResource {x:Static MenuItem.SubmenuItemTemplateKey}}" />
</Trigger>
</Style.Triggers>
</Style>
</Menu.ItemContainerStyle>
<MenuItem Header="Just Item" />
<MenuItem Header="Maths">
<MenuItem Header="Add" />
<MenuItem Header="Subtract" />
</MenuItem>
<MenuItem Header="Misc">
<MenuItem Header="Other" />
</MenuItem>
</Menu>