Question

My problem is reproducible on following example. Simple WPF window:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="525"
        Height="350">
    <StackPanel>
        <ToolBar>
            <TextBox Name="textbox1"
                     Width="100"
                     Text="test1" />
        </ToolBar>
        <TextBox Name="textbox2"
                 Width="100"
                 Height="25"
                 HorizontalAlignment="Left"
                 Text="test2" />
    </StackPanel>
</Window>
  1. Click to TextBox with text 'test2'
  2. Click to TextBox with text 'test1'
  3. textbox1 got text cursor and is blinking (normal state). But text cursor also stayed in textbox2 and is not blinking - this is weird.

Why isn't removed text cursor from textbox2?

Result

Was it helpful?

Solution

Try this

    <StackPanel FocusManager.IsFocusScope="True">
        <ToolBar>
            <TextBox Name="textbox1" Width="100" Text="test1" />
        </ToolBar>
        <TextBox Name="textbox2" Width="100" Height="25" HorizontalAlignment="Left" Text="test2" IsInactiveSelectionHighlightEnabled="True"/>
    </StackPanel>

for more details visit

OTHER TIPS

This is the normal behaviour in WPF... more than one control can have logical focus at one time, depending on where it is, or more specifically on the focus scope that it belongs to. Conversely, only one control at a time can have the keyboard focus. For example, it's perfectly legal to have logical focus remain on a selected MenuItem for example, while somewhere else the logical and keyboard focus can be highlighting the selected item in a ListBox.

You can find out the full story in the Focus Overview page on MSDN. From the linked page:

Logical focus refers to the FocusManager.FocusedElement in a focus scope. A focus scope is an element that keeps track of the FocusedElement within its scope. When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top