Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically select the focused element?

Tags:

c#

wpf

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?

enter image description here

like image 424
Waterfr Villa Avatar asked Nov 10 '15 23:11

Waterfr Villa


People also ask

How do you find the ID of a focused element?

It can be used to get the currently focused element in the document: Syntax: var ele = document. activeElement; Return value: It returns the currently focused element in the document.

What elements can be focused HTML?

Elements of the following type are focusable if they are not disabled: input , select , textarea , button , and object . Anchors are focusable if they have an href or tabindex attribute. area elements are focusable if they are inside a named map, have an href attribute, and there is a visible image using the map.


Video Answer


2 Answers

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.

like image 64
Rowbear Avatar answered Oct 03 '22 05:10

Rowbear


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.

like image 36
tgpdyk Avatar answered Oct 03 '22 05:10

tgpdyk