Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8316365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:20:50+00:00 2026-06-08T21:20:50+00:00

In a control I’m currently working on, when an item is focused, the default

  • 0

In a control I’m currently working on, when an item is focused, the default focus rectangle spans the entire row and all of its visible child items. I know how to hide that. But I still want such a focus indicator when the item has the keyboard focus. I’ve read about the IsKeyboardFocused property but that is true also when the item was clicked by the mouse. So I guess I need to use the FocusVisualStyle somehow. But I can’t figure out how.

Here’s what the default focus looks like:

enter image description here

And this is what it should like:

enter image description here

Here’s my XAML code for the control template:

<Border ...>
    <ContentPresenter FocusManager.IsFocusScope="True"
        Content="{TemplateBinding HeaderedContentControl.Header}"
        ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
        ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
        ContentSource="Header"
        Name="PART_Header" .../>
</Border>
<!-- Additional border glare inside the item -->
<Border BorderThickness="1" BorderBrush="#80ffffff" Margin="1"
    SnapsToDevicePixels="True" CornerRadius="2"/>
<!-- Focus rectangle inside the item -->
<Rectangle StrokeDashArray="1 2" StrokeThickness="1" Stroke="Black"
    SnapsToDevicePixels="True" Margin="2"
    Visibility="Hidden" Name="FocusRectangle"
    FocusVisualStyle="{StaticResource FocusStyle}"/>

There is already a focus rectangle in my XAML that is invisible by default. With FocusVisualStyle or anything, it shall be made visible. But I didn’t manage to do that. It’s either visible at any focus, or never.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:20:51+00:00Added an answer on June 8, 2026 at 9:20 pm

    I have found a workaround for the issue. It looks the same to me, but I’m not completely sure whether it’s the correct way to do it. I’m using the FocusRectangle from above and care for showing and hiding it myself.

    Here’s the trigger that manages the focus rectangle visibility:

    <!-- Show the focus rectangle when the item is focused -->
    <MultiTrigger>
      <MultiTrigger.Conditions>
        <Condition Property="Controls:TreeViewExItem.IsKeyboardMode" Value="True"/>
        <Condition Property="Controls:TreeViewExItem.IsFocused" Value="True"/>
      </MultiTrigger.Conditions>
      <Setter TargetName="FocusRectangle" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
    

    Then I have added a new property to the TreeViewExItem that indicates whether the last input came from the mouse or the keyboard. This could possibly be extended to touch or stylus, but I don’t have such devices to test.

    public static DependencyProperty IsKeyboardModeProperty =
        DependencyProperty.Register(
            "IsKeyboardMode",
            typeof(bool),
            typeof(TreeViewExItem),
            new FrameworkPropertyMetadata(false, null));
    
    public bool IsKeyboardMode
    {
        get
        {
            return (bool) GetValue(IsKeyboardModeProperty);
        }
        set
        {
            SetValue(IsKeyboardModeProperty, value);
        }
    }
    

    This property is passed to each item from the parent control through binding:

    <!-- Pass on the TreeViewEx' IsKeyboardMode value to each item because
      we couldn't access it otherwise in the triggers -->
    <Setter Property="IsKeyboardMode"
      Value="{Binding (Controls:TreeViewEx.IsKeyboardMode),
        RelativeSource={RelativeSource
          AncestorType={x:Type Controls:TreeViewEx}}, Mode=OneWay}" />
    

    The same IsKeyboardMode property is added to the TreeViewEx parent control and here comes my magic:

    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e);
        if (!IsKeyboardMode)
        {
            IsKeyboardMode = true;
            //Debug.WriteLine("Changing to keyboard mode from PreviewKeyDown");
        }
    }
    
    protected override void OnPreviewKeyUp(KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e);
        if (!IsKeyboardMode)
        {
            IsKeyboardMode = true;
            //Debug.WriteLine("Changing to keyboard mode from PreviewKeyUp");
        }
    }
    
    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseDown(e);
        if (IsKeyboardMode)
        {
            IsKeyboardMode = false;
            //Debug.WriteLine("Changing to mouse mode");
        }
    }
    

    This reacts on the preview events of keyboard and mouse to set the appropriate input mode. Only if the last input came from a keyboard, the focus rectangle is visible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A control reports its display rectangle in .DisplayRectangle -- what is the simplest way
I have a machine control system in Python that currently looks roughly like this
(Control Panel\All Control Panel Items\Credential Manager) How would I access Windows credentials from a
to control my input type and verify that its value is pretty a float
The control below draws a string in a rectangle. On mouse move there is
Edit Control's in my Win32 App just refuse to take focus!. As a result,
I have a GridView control which shows a list of all employees. When user
Control Panel -> Security Center I really like the components/controls which are used to
DockingManager control has been removed from their suite in Q1 2011, so now I
foreach (Control c in panPrev.Controls) { if (c.Tag == move) works well, but produce

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.