WPF Keyboard Focus and Tabbing - wpf

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.

Related

WPF MenuItem usability by mouse

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?

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

How can I make JAWS read the contents of a UserControl the same way it does a Window?

JAWS (the screen reader) is treating the contents of a WPF user control differently than the contents of a window. The first time a user tabs to a focusable element in a user control, JAWS reads the entire contents of the user control instead of only the focused element. On a window, when the user tabs to a control, JAWS only reads the focused control. How can I make the user control behave like the window?
Consider the following XAML (namespaces/designer declarations omitted for brevity):
<Window ... Title="MainWindow">
<StackPanel>
<Label>First label</Label>
<Button>A button</Button>
<Label>Second label</Label>
<CheckBox>A checkbox</CheckBox>
</StackPanel>
</Window>
When the above WPF application is started, JAWS reads "Main Window". If the user presses Tab, the first button is focused and JAWS reads "Tab A Button button to activate press space bar". This is expected.
Now consider the above controls wrapped into a user control:
<UserControl ...>
<StackPanel>
<Label>First label</Label>
<Button>A button</Button>
<Label>Second label</Label>
<CheckBox>A checkbox</CheckBox>
</StackPanel>
</UserControl>
and then the user control is placed on MainWindow to replace the other controls:
<Window ... Title="MainWindow">
<uc:FirstUserControl/>
</Window>
Now when the app is started, JAWS reads the same thing ("Main Window"), but when the user presses Tab, JAWS reads: "Tab First label Second label A Button to activate press space bar". If the user presses Tab again, the checkbox receives focus but JAWS does not re-read everything - it only reads the checkbox label, as expected. Pressing Tab a third time will cycle back to the button, and this time JAWS only reads the focused button text. It seems to only read the entire contents of the user control the first time a child control receives focus.
How can I make the contents of user controls behave the same as controls on a normal window? (That is, I want JAWS to only read the focused content unless the user tells it to read everything on the page).
You can set AutomationProperties.Name=" " on the usercontrol.
You can also override OnCreateAutomationPeer method of user control to return null.

Error template get lost when changing tab in MahApps.Metro

In my window I have 2 tabs. One with the main informarion and the other one with an editable grid. When I click the save button the entire window gets validated and the controls with validation problems are highlighted in red. But if i change the tab to the one with the grid and get back to the one with the validation problems the controls are not highlighted anymore. Even if i click the save button again, and the validation happens, the controls never get highlighted again.
Here are the screen captures
it's a bit late, but today I had the same situation and found the solution.
You have to add a AdornerDecorator inside the TabItem:
<TabItem Header="Foo">
<AdornerDecorator>
//more content
</AdornerDecorator>
</TabItem>
I have found it in a more general question:
TextBox with validation loses ErrorTemplate on tab change
Greetings

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