WPF MenuItem usability by mouse - wpf

WPF MenuItem is only selected when user hovers mouse exactly over the text of MenuItem.
If user clicks now then menu will be expanded, good:
This does not happen when mouse is not over the text.
If user clicks in this situation then nothing happens (you can see background of MenuItem is darkened, i.e. mouse is not over the element):
This is quite incovenient, uses complained, I even set bold font to increase area where user clicks succeed to open submenu.
Code is very simple:
<MenuItem x:Name="MainMenuItem" Header="Menu" ToolTipService.HasDropShadow="True"
Foreground="{StaticResource BasicBritnessForeground}" FontWeight="Bold">
How to fix this?

Related

Command is invoked only if user press on particular area of button content

I am a newbe on wpf. I need a control like that
It should work like a button. It should change background color on IsMouseOver and on IsPressed. But click should be triggered and command invocked only when user clicks the cross. Is there a way to restrict clickable area for button with providing some custom content or template?
Instead of trying to make a whole button-control with just a "partly"- button I would instead do it with a stackpanel or grid.
<StackPanel Orientation="Horizontal">
<Textblock>Some Text</Textblock>
<Button source="YourImage"/>
</StackPanel>
Now you can decide width,heigt and those properties you see fit. To make your Textblock and button to change background if the mouse is over, I would look over Style's and Trigger's
Here is a link to a tutorial

WPF giving keyboard focus to user control

I have a window that displays a number of user controls. One of these needs to expose some keyboard shortcuts, which I've defined like so:-
<UserControl.InputBindings>
<KeyBinding Key="N"
Modifiers="Control"
Command="{Binding AddCommand}" />
..etc..
</UserControl.InputBindings>
The user control itself has a variety of child controls - TextBoxes, a DataGrid, buttons, etc. The keyboard shortcuts will be used to manipulate items in the DataGrid.
When I click on the UC, the keyboard shortcuts may or may not work, depending on where I've clicked. E.g. they won't work after clicking on the DataGrid, but will work after clicking on a TextBox or Button.
I'd assumed that clicking anywhere on the UC would have naturally given it focus. Is it something to do with keyboard focus and mouse focus being two different things? Not really had to handle this sort of thing before now.
How do I get the keyboard shortcuts to work - either when the UC first appears, or whenever the user clicks anywhere on it?

WPF Keyboard Focus and Tabbing

If I have a group of buttons as a wpf control, for example:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Button Content="Button 1"/>
<Button Content="Button 2"/>
</StackPanel>
</UserControl>
and run this application, Button 1, as expected, has focus on start-up, but with no FocusVisualStyle, i.e., dotted line surrounding the button. Pressing the tab key gives Button 2 focus, and gives it FocusVisualStyle. Pressing tab again gives focus back to Button 1 with FocusVisualStyle.
Here's the behaviour I'm after: On start-up, Button 1 should have focus with no FocusVisualStyle. Pressing tab keeps focus on Button 1 and gives it FocusVisualStyle (not Button 2). Pressing tab again cycles through the buttons giving each focus and FocusVisualStyle accordingly.
Ideally I want to achieve this in XAML, but not sure it is possible having tried various combinations of setting the focused element via the focus manager. Any suggestion how this might be achieved?
Its hacky way but should do the trick for you if you dont have any Default buttons in that page.
Just set
IsDefault="True"
for the first button. If this interferes with any other control behaviors in your screen then you have to write a behavior in XAML for adding removing IsDefault on tabs.

WPF: How to change button content (text) with a double click at runtime

I have a button in WPF, I want to change the text when I double click on it, that is I want the cursor to appear and type the text that is to be shown as the content (similar behavior as when pressing F2 on a desktop shortcut).
I guess I could detect a double click and then show a textbox with a transparent background, that will get me the cursor, type the text in this new textbox, set it to the buttons content and delete the textbox, but that doesn't seem the right way to do it.
I guess what I had in mind, is that I am developing a diagramming tool using shapes. Since shape doesn't derive from ContentControl I cannot put a text box inside it, and I want to simulate this behavior. I was thinking of making a custom control but that might be too much work for this, and am not quite familiar with this topic yet. I guess another approach would be to use an adorner (maybe a border) and since it derives from contentcontrol I can do the same thing as joe suggested. any ideas?
Another thing I could do would be to put the shape in a grid, and then put the textbox on top of the shape, but I am not sure how would that be as a design principle, and also I don't know if the hit testing would only be on the shape or the grid.
Since this is WPF, you can put a TextBox inside your Button with no trouble. If you don't want the textbox to have a border and white background -- i.e., if you want it to look like you're just typing directly into the button -- then you could remove them by setting BorderWidth to 0 and Background to Transparent.
What you probably want to do is have your Button's Content be a Grid that contains both the normal content (probably a TextBlock) and the TextBox, with the TextBox initially hidden (Visibility = Collapsed). Then when you get the double-click event, you would hide the TextBlock and show the TextBox.
<Button>
<Grid>
<TextBlock Name="buttonText">Double-click to rename me</TextBlock>
<TextBox Name="buttonEdit" Visibility="Collapsed" MinWidth="100"/>
</Grid>
</Button>

Non-Modal floating dialog in WPF

I want to create a UI sequence where the user clicks a button and it pops up a small panel below it with a button and a textbox and maybe a small list of items.
The dialog is non-modal and more importantly, it just goes away when you click somewhere else in the main window.
For example, when you click the 'Favorites' Star icon in Internet Explorer 7 or you click the Star in the location bar in Firefox twice and it brings up the bookmark editor dialog.
What's the cleanest way to achieve this?
Should I use a UserControl and absolutely fix the location of it when a button is clicked?
If so, how do I hide it when the user clicks somewhere else?
I'd say the cleanest way to do what you are looking for is to use a Popup. The Popup class displays an element that floats above the rest of the elements on the screen, but is non-modal and can be configured to disappear when the user clicks away from it - perfect for your non-modal dialog. The Popup class has properties that allow you to control where it shows up relative to another control (in your case, the button you want the user to press to open the popup).
Here's an all-XAML example:
<Grid>
<ToggleButton HorizontalAlignment="Center" VerticalAlignment="Top"
x:Name="PopButton" Content="Pop"/>
<Popup Placement="Bottom" PlacementTarget="{Binding ElementName=PopButton}" StaysOpen="False"
IsOpen="{Binding ElementName=PopButton, Path=IsChecked, Mode=TwoWay}">
<Rectangle Height="100" Width="200" Fill="Blue"/>
</Popup>
</Grid>
You can also use commands or event handlers to open/close the popup from code.
The Placement and PlacementTarget properties set where the popup will appear, and which control it will appear relative to (there are other options that allow you to have it appear relative to its current position and relative to the mouse, too). Setting StaysOpen to False will have WPF automatically close the popup when the user clicks outside of it.
By default, a Popup has no style of it's own - it's just a container for floating content - so you'll have to style it to look like your window chrome/toolbar/etc. as appropriate.

Resources