none
FocusManager.IsFocusScope="True" leads to unexpected results RRS feed

  • Question

  • Hi,

    for our application I have implemented MDI-support. To be able to keep track of the focused elements in each MDI-child I tried to set FocusManager.IsFocusScope to true in the child-window.

    Keeping the logical focused element and setting the keyboard-focus to it when the window is focused would be the expected behaviour (according to the MSDN-Library). Instead the focus always escapes from the child-window immediately after clicking on any element in it. For example, if I click on a button in my MDI-child, it is focused during mouse-click, but after MouseUp it disappears, so that the previously focused element outside my focus-scope got the focus.

    As far as I know the Window-class also has IsFocusScope set to true. Fortunately this is able to somehow keep the focus... (sorry, I am a bit upset...)

    I must admit that I have read about this problem in another thread of this forum. But the answers don´t satisfy my. Handling with logical focus should be different.

    Try the following as example:
    &lt;Window x:Class="WpfApplication3.Window1"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Title="Window1" SizeToContent="WidthAndHeight"&gt;<br>&nbsp;&nbsp;&nbsp; &lt;StackPanel&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ContentControl FocusManager.IsFocusScope="False"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Border BorderBrush="Black" BorderThickness="1"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;StackPanel Orientation="Horizontal"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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sp; &lt;ContentControl FocusManager.IsFocusScope="True"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Border BorderBrush="Black" BorderThickness="1"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;StackPanel Orientation="Horizontal"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button Name="bt3" Margin="12"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Button 3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Button&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button Name="bt4" Margin="12"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Button 4<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Button&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TextBlock Text="Container has IsFocusScope='True'" <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VerticalAlignment="Center" Margin="12" /&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/StackPanel&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Border&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ContentControl&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/StackPanel&gt;<br>&lt;/Window&gt; 


    How can I proceed to get the behaviour that FocusManager should have?

    Thanks for any reply,
    Alex
    Tuesday, October 14, 2008 9:03 AM

Answers

  • Setting IsFocusScope to true is horribly broken. Focus scopes do NOT do (or rather, do a lot more undesirable things than) what MSDN appears to say they do. Many people have tried to use them properly, but they just don't work as they should.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Marco Zhou Monday, October 20, 2008 10:12 AM
    • Marked as answer by Marco Zhou Monday, October 20, 2008 10:12 AM
    • Marked as answer by Marco Zhou Monday, October 20, 2008 10:13 AM
    Tuesday, October 14, 2008 10:37 AM

All replies

  • Haha, "Format Code Block" still doesn´t work.

    Here it is:
    <Window x:Class="WpfApplication3.Window1" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            Title="Window1" SizeToContent="WidthAndHeight"
        <StackPanel> 
            <ContentControl FocusManager.IsFocusScope="False"
                <Border BorderBrush="Black" BorderThickness="1"
                    <StackPanel Orientation="Horizontal"
                        <Button Name="bt1" Margin="12"
                            Button 1 
                        </Button> 
                        <Button Name="bt2" Margin="12"
                            Button 2 
                        </Button> 
                    </StackPanel> 
                </Border> 
            </ContentControl> 
            <ContentControl FocusManager.IsFocusScope="True"
                <Border BorderBrush="Black" BorderThickness="1"
                    <StackPanel Orientation="Horizontal"
                        <Button Name="bt3" Margin="12"
                            Button 3 
                        </Button> 
                        <Button Name="bt4" Margin="12"
                            Button 4 
                        </Button> 
                        <TextBlock Text="Container has IsFocusScope='True'"  
                                   VerticalAlignment="Center" Margin="12" /> 
                    </StackPanel> 
                </Border> 
            </ContentControl> 
        </StackPanel> 
    </Window> 

    Tuesday, October 14, 2008 9:04 AM
  • Setting IsFocusScope to true is horribly broken. Focus scopes do NOT do (or rather, do a lot more undesirable things than) what MSDN appears to say they do. Many people have tried to use them properly, but they just don't work as they should.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Marco Zhou Monday, October 20, 2008 10:12 AM
    • Marked as answer by Marco Zhou Monday, October 20, 2008 10:12 AM
    • Marked as answer by Marco Zhou Monday, October 20, 2008 10:13 AM
    Tuesday, October 14, 2008 10:37 AM
  • Thank´s for your reply, Tim! Does anybody know if MS plans to fix this?
    Tuesday, October 14, 2008 12:29 PM