MenuItem Header starts with _ - wpf

I have some contextMenu which shows list of object.
My problem is when one of the objects starts with underline ("_") for example : _obj1,
and the results is that the mentitem displayed without the underline : obj1".
Any idea?

I fix it by changing the header template.
<Style TargetType="{x:Type MenuItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Header, RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}}}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

I think you may miss a feature provided by wpf when using a underline-start name.
When you use _obj1, you get the menutime displayed as obj1, but you should try press ALT then you will see the obj1 "underlined" and you can use ALT+o (the first character of the obj name) to "press" the menuitem without using a Mouse.
For example, when you write this:
<MenuItem Header="_File">
<MenuItem .../>
</MenuItem>
Then you just see the Menu with "File", but you can use ALT+F as you are clicking this MenuItem.
Just try this and you shall understand.

Related

Set scroll to one of the MenuItem on a contextmenu in WPF

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?

ContextMenu (on right click) on GridView Header Line

I am looking on how to add a ContextMenu on the header of my GridView.
I don't want to add it on the Column's header, but on the full "line" where are all my headers. So even if the user hides all columns, the tooltip will still be available.
Concretely, I want to be able by right-clicking in the headers line, to make appear a ToolTip, containing a list of Comboboxes, corresponding to which columns I want to see or hide.
For now I only found how to launch an event on right-click on header (GridViewColumnHeader.MouseRightButtonDown), but then I have no idea how to go further.
You can use the GridView.ColumnHeaderContainerStyle to set the GridViewColumnHeader.ContextMenu property:
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem>
<MenuItem.Header>
<ComboBox />
</MenuItem.Header>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</GridView.ColumnHeaderContainerStyle>
</GridView>

Why does only part of the context menu trigger the associated command in WPF?

I have a need to bind context menu items to a list in my view model. It works and the code is rather simple, but there is one problem that my users won't accept. You have to click in a certain area of the context menu in order to get the command to trigger.
If you click outside of the darker blue area then the command does not trigger.
The XAML is as follows:
<ContextMenu ItemsSource="{Binding MyContextMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Header}" Command="{Binding Command}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
It appears that the dark blue is the actual MenuItem control, but I need the parent to be the clickable area, I would also prefer that it was all highlighted the same, but I think That that won't be very difficult to figure out once I know the solution to the main issue.
Thanks to DRapp's comment I figured out the solution to my issue. The menu item is actually created automatically and I was creating an menu item inside of the one that was created for me. The solution was to define the ContextMenu.ItemContainerStyle instead of using the ContextMenu.ItemTemplate. Below is the final XMAL.
<ContextMenu ItemsSource="{Binding MyContextMenuItems}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="Command" Value="{Binding Command}" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>

MenuItem.Header and MenuItem.Icon DataTemplate

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? :)

Silverlight: Why doesn't this style work?

I'm using Silverlight 4. I have a button:
<Button Click="addTopicButton_Click">
<Image Source="/PlumPudding;component/Images/appbar.add.rest.png" />
</Button>
It looks fine. However, when I try to set its Content using a Style, no content appears:
<Style x:Name="AddButton" TargetType="Button">
<Setter Property="Content">
<Setter.Value>
<Image Source="/PlumPudding;component/Images/appbar.add.rest.png" />
</Setter.Value>
</Setter>
</Style>
<Button Click="addTopicButton_Click" Style="{StaticResource AddButton}" />
The button is empty. Why is this?
Its not a good idea to include UIElements such as Image in a style. Such an object is created only once when the style is put together during Xaml parsing. An important thing to understand about UIElements is that a single instance can only appear once in the Visual Tree. So even if your code worked it would only work for one button, any other button trying to use the same style would fail.
Instead you can use the ContentTemplate property like this:-
<Style x:Key="AddButton" TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="/PlumPudding;component/Images/appbar.add.rest.png" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Click="addTopicButton_Click" Style="{StaticResource AddButton}" />
The button is now given a DataTemplate that it uses to construct the child element that renders the content of the button. Each button will therefore construct its own independent instance of an Image control.
You should use x:Key to name your Style element rather than x:Name
Your code requires two changes.
Changed x:Name to x:Key, And refer to it when you want to use it, using StaticResource.
Change this
<Setter.Value>
<Image Source="whatever..." />
</Setter.Value>
to this,
<Setter.Value>
<DataTemplate>
<Image Source="whatever..." />
</DataTemplate>
</Setter.Value>
See if it helps you!

Resources