Reputation: 1317
There is a page on which there are several focusable elements(Buttons, Images, ...) generated some statically in XAML, some dynamically in code behind. On this page, pressing the tab key will make the elements focused one by one. That means the dotted lined gets displayed around the current element. Now, I would like to make the current focused element, selected too. That means to display the blueish line around it too. So as the focused moves, so does the selected
How can I do that in the C# code-behind?
Upvotes: 14
Views: 1468
Reputation: 431
What I did in the xml. I added PreviewGotKeyboardFocus to my window
PreviewGotKeyboardFocus="FrmBase_OnPreviewGotKeyboardFocus"
Use PreviewGotKeyboardFocus on the root element (usercontrol or window) to handle the focus event on every child control.
Code behind
private void FrmBase_OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
try
{
dynamic item = e.NewFocus;
item.IsSelected = true;
}
catch (Exception ex)
{
}
}
Upvotes: 2
Reputation: 804
I would ensure that every element is either a button or the template of a button. This way the "IsTabStop" can be used, or the elements can be focused. Using the solution of MrsEd you can get the item to be correctly bordered:
<Button x:Class="Visualisation.UserControlButtonImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
IsTabStop="True">
<Button.Template>
<ControlTemplate>
<Grid>
<Image/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
Upvotes: 3
Reputation:
The type of elements you are referring to, are not all selectable.
You want this in C#, and some elements will be added dynamically, and the keyboard will tab through the elements. I suggest keeping track of your focused items and then implementing a method that does whatever it is you are trying to do based on the current focused element. Using the keyboard focus method.
protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
var focusedElement = e.Source as IInputElement;
// Do something here
}
Upvotes: 1
Reputation: 1669
I would say the best way to do it perhaps varies depending on what kinds of focusable elements you have? If you want to do this for listboxitem's, you can do this with only xaml like so:
<UserControl.Resources>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Selector.IsSelected" Value="True"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<ListBox>
<ListBoxItem>
Blah
</ListBoxItem>
</ListBox>
A similar style trigger can be applied for other focusable and selectable elements as well.
Upvotes: 10
Reputation: 1233
Edited:
I think what you just need is change the dotted line on TabStop. That broken line is indicative of selected state. So, it is selected already. This 'select the focused element' statement does not make any sense since if you are referring to TabStop, it is indeed already selected. You can test it by pressing the Tab and then whichever has the dotted line, if it is a button and hit enter, it will do the Click event (if there is ever a handler behind it).
What you need is this.
If you want to do it in code behind, add this to the Resource of your XAML.
<Style x:Key="MyFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-2" StrokeThickness="1" Stroke="Blue" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
....
myButton.FocusVisualStyle = (Style)FindResource("MyFocusVisual");
If you do not have an access to the XAML, I think from this, you can figure out how to add a style in code-behind and add the style to the button. Now, I am obviously contradicting myself here, since you are aiming for a code-behind, it is impossible not to have access to the XAML right? It is still practical to add this via XAML than code behind.
Upvotes: 4