How do I make custom MenuHeaders in WPF with accelerators? - wpf

I'd like to make some custom MenuHeaders in WPF so I can have (for example), an icon and text in a menu item.
Normally using MenuItems, if you populate the Header field with straight text, you can add an accelerator by using an underscore. eg, _File
However, if I wanted to put in a UserControl, I believe this function would break, how would I do something similar to the following?
<Menu>
<MenuItem>
<MenuItem.Header>
<UserControl>
<Image Source="..." />
<Label Text="_Open" />
</UserControl>
</MenuItem.Header>
</MenuItem>
...

I think the Icon property fits your needs.
However to answer the original question, it is possible to retain the Accelerator functionality when you compose the content of your menuitem. If you have nested content in a MenuItem you need to define the AccessText property explicitly like in the first one below. When you use the inline form, this is automagically taken care of.
<Menu>
<MenuItem>
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="Images/Open.ico" />
<AccessText>_Open..</AccessText>
</StackPanel>
</MenuItem.Header>
</MenuItem>
<MenuItem Header="_Close" />
</Menu>

The problem is you placed the image inside of the content of the MenuHeader which means that you'll lose the accelerator key. If you're just trying to have an image in the menu header, do the following.
<MenuItem Header="_Open">
<MenuItem.Icon>
<Image Source="images/Open.png"/>
</MenuItem.Icon>
</MenuItem>
If you want to customize the look and feel even further, modify the controltemplate and style for the menu. From experience, styling the menus and menuitems are much more difficult then styling the other WPF controls.

First thought, you would think that the Icon property can only contain an image. But it can actually contain anything! I discovered this by accident when I programmatically tried to set the Image property directly to a string with the path to an image. The result was that it did not show the image, but the actual text of the path! Then I discovered that I had to create an Image element first and set that to the Icon property. This lead me to think that the Image property was just any content container that is located in the icon area at the left in the menu, and I was right. I tried to put a button there, and it worked!
This is showing a button with the text "i" in the Icon area of the menu item. When you click on the button, the Button_Click event is triggered (the LanguageMenu_Click is NOT triggered when you click the button).
<MenuItem Name="LanguageMenu" Header="_Language" Click="LanguageMenu_Click">
<MenuItem.Icon>
<Button Click="Button_Click">i</Button>
</MenuItem.Icon>
</MenuItem>
This leads to an alternative to not have to make an image for the icon, but use text with a symbol font instead to display a simple "icon". The following example uses the Wingdings font which contains a floppydisk symbol. This symbol in the font is mapped to the charachter <, which has special meaning in XAML, so we have to use the encoded version < instead. This works like a dream! The following shows a floppydisk symbol as an icon on the menu item:
<MenuItem Name="mnuFileSave" Header="Save" Command="ApplicationCommands.Save">
<MenuItem.Icon>
<Label VerticalAlignment="Center" HorizontalAlignment="Center"
FontFamily="Wingdings"><</Label>
</MenuItem.Icon>
</MenuItem>

#a7an: Ah, I didn't notice the Icon property before. That's a good start.
However, specifically I wanted to add an extra 'button' to some MenuItems so I could have a 'Pin' feature (see the recently loaded Documents list in Office 2007 for the feature idea).
Since there needs to be code as well, will I probably need to subclass the control and add the code for the button? (Not affraid of messing with the MenuItem template, have already had to do it once and I'd do it again if I had to! ;) )

Related

Combo box behaviour incorrect when placed on the menu bar

I have a question - why is the behaviour of the comboBox different when it is located on a menu bar like below?
The problem is that when I click the comboBox (to provide the URL address) I cannot type - it seems that focus is immediately lost. I have to click on the dropdown list icon, and only then I can type in.
If I move this ComboBox outside the menu bar, I can use it the way I want.
<Menu Height="auto" Name="menu1" VerticalAlignment="Top">
<ComboBox Name="urls_list" IsEditable="True" Width="220" Height="auto" Loaded="urls_list_Loaded"></ComboBox>
<MenuItem Header="Options"></MenuItem>
<MenuItem Header="About"></MenuItem>
</Menu>
To give you a little background - I want to have a menu bar on top of the window with a few menu buttons and the "address bar" that allows to both type new addresses or use the predefined ones, but it is very annoying that you can't start typing in right away.
How can I achieve that?

how to show image along with conent on a button in wpf using mvvm [duplicate]

This question already has answers here:
Button template with image and text in wpf
(7 answers)
Closed 8 years ago.
I am creating one sample WPF-MVVM application , in that I have one image which indicates '+' sign and I have a button with content 'Edit'. Now I have to show the image along with Name 'Edit' on a button. Please let me know the solution for this problem.
EDIT
here I am able to show the image but the name is appered below the image. But I want to show the name beside the image.
you can do something like this
<Button Margin="104,78,84,60" Name="button1" Height="100" Width="200">
<StackPanel Orientation="Horizontal">
<Image Source="ssv.png" Stretch="None" Height="50" Width="50" />
<TextBlock TextAlignment="Center">Text value for this.</TextBlock>
</StackPanel>
</Button>
There are a few ways you could create an 'image button', one way would be to create a custom which derives from Button and adds an Image property, or a user control that uses a Button and declaratively adds the image as part of the buttons's control template in the user control XAML.
However, these approaches mean that the layout of the image within the button is fixed, so it isn't particularly flexible.
A nicer option is to create an attached property which stores the image location, and then reference this attached property value in the buttons control template. You can then create a style for the control template to make the layout reusable across buttons.

WPFToolkit:Split button dropdown button disable button

I am using WPF split button which is inherited from
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
The issue is I want to disable , the button ' the one below(Right side of Button 'Conf' in below 'downarrow present in the below fig) , If user clicks on Left side of the button the rightside should be disabled and the button(leftside) background should change to yellow.please find below the xaml , I am using the wpf split button , dropdown content in this case .please let me know if you have any idea
<extToolkit:SplitButton x:Name="ABCbutton"
VerticalAlignment="Center"
Command="{Binding ACommand}"
FontSize="16>
<TextBlock HorizontalAlignment="Center"
IsEnabled="{Binding IsEnabled, ElementName=AButton}"
Text="A"/>
<extToolkit:SplitButton.DropDownContent>
<StackPanel>
<Button Command="{Binding BCommand}"
Padding="3"
Style="{DynamicResource
DropDownButtonMenuButton}">
<TextBlock Margin="0,3,6,3"
Text="B"/>
</Button>
<Button Command="{Binding BCommand}"
Padding="3"
Style="{DynamicResource
DropDownButtonMenuButton}">
<TextBlock Margin="0,3,6,3"
HorizontalAlignment="Stretch"
Text="C"/>
</Button>
</StackPanel>
</extToolkit:SplitButton.DropDownContent>
</extToolkit:SplitButton>
In a case like this, you will likely be better off creating your own control. The split button can't really handle what you are talking about without a good deal of modification.
Essentially your new control will consist of two buttons, visually styled so that they look like they blend together. Each will have it's own background and enabled property. Add a ViewModel to help control behaviors and set properties, and I think you would be in business. Keep in mind that all of the controls that are supplied in any of the toolkits, frameworks, control packs, etc are made with the primitive types of drawing and framework elements (line, rectangle, border, content presenter, panel, etc) with specialized behavior code.
I end up making a lot of custom controls just because there is not one that perfectly replicates what I want.

Easy way to have checkmark below image for WPF checkbox?

In a WPF window I got a CheckBox containing an image, like this:
<CheckBox>
<Image Source="/Bsoft.Clients.Warenwirtschaft;component/Resources/MyImage.png" />
</CheckBox>
Using this, the checkmark is displayed left to the image.
Is there an easy way to move the checkmark to below the image?
I do not know of an easy way to do this.
You could either use some panel and have a checkbox independent from your image...
<StackPanel>
<Image .../>
<CheckBox .../>
</StackPanel>
Which might not be too much trouble if encapsulated in a UserControl.
Or you change the ControlTemplate of the CheckBox, which is more difficult i think. Default templates can be found on MSDN.

Correct way to create a menu with shortcuts in WPF

What is the correct/best way to create a menu with hotkey shortcuts?
I simply want a File menu like Visual Studio's that has New, Open, Save, Save All, Exit, and a few other standard shortcuts.
It seems that InputGestureText displays the appropriate text, but since it's called "Text" and doesn't seem to trigger events, I'm going to assume that isn't the right way to do it. The Command architecture also seems fairly bulky, so I don't want to head down that path if there is a better way.
Update:
For clarity, let's say I'm using the following menu:
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_New" />
<MenuItem Header="_Open" />
<Separator />
<MenuItem Header="_Save" />
<MenuItem Header="Save _All" />
<Separator />
<MenuItem Header="_Export"/>
<MenuItem Header="_Import"/>
<Separator />
<MenuItem Header="E_xit"/>
</MenuItem>
</Menu>
where Export and Import would be custom shortcuts; something that Microsoft didn't build into something like ApplicationCommands. Can you please provide a complete solution, including C# custom code if necessary? (Not only will it help me out, but I hope to help anyone else out who is searching for similar issues.)
You are correct in noting that InputGestureText does not actually set up a shortcut, it simply labels it on the menu. The correct way to do it is to use the InputBindings of the window. You can map a keyboard shortcut to any command, including your own model-defined commands. But in order for this to work, you need to use menu items bound to commands - not handle their click events directly. This is the recommended way of handling menu commands in WPF. Otherwise you'll need to resort to old fashioned keyboard event handling.
<Window.InputBindings>
<KeyBinding Key="A" Modifiers="Control" Command="{Binding MyAwesomeCommand}" />
</Window.InputBindings>
More information on MSDN.
Take a look at the ApplicationCommands class. You get the standard key gestures (accelerators) and text for free.
<MenuItem Command="ApplicationCommands.Paste" Width="75" />

Resources