Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
XamTextEditor "IsFocused" Compared to Textbox "IsFocused"
posted

When I use the following Xaml code, I get different results between a WPF textbox and XamTextEditor.  If I select a textbox either by clicking or tabbing to it, the properties are changed as indicated in the code.  With XamlTextEditor, these changes occur after I hit the enter key after getting the focus of the XamTextEditor. Shouldn't the XamTextEditor mimic the textbox?

<Trigger Property="IsFocused" Value="True">

<Setter Property="BorderBrush" Value="DarkOrange"/>

<Setter Property="Background" Value="Black"/>

<Setter Property="BorderThickness" Value="4"/>

</Trigger>

  • 2070
    posted

     Hi,

     

    The reason why IsFocused doesn't work with XamTextEditor is because XamTextEditor contains a textbox inside it for editing purpose. When it recieves focus, it passes the focus along to the textbox. This means that when you enter into a XamTextEditor, the textbox inside it ends up with the focus and not the XamTextEditor itself. IsFocus property doesn't test for whether a descendant element is focused. It simply checks for whether the element itself is focused and that's why IsFocus would return false even if the textbox inside it has focus.

    Instead of using IsFocused, you can use the FocusWithinManager class provided in the Infragistics.Windows assembly. FocusWithinManager's IsFocusedWithinProperty returns a value indicating whether the control itself or any of its descendants have focus. Here's the relevant code:

     

    // Either use an existing class or define a new class and define IsFocusWithinProperty
    // by adding an owner to FocusWithinManager.IsFocusWithinProperty.
    public class FocusWithinHelper : DependencyObject
    {
        public static DependencyProperty IsFocusWithinProperty =
            Infragistics.Windows.Helpers.FocusWithinManager.IsFocusWithinProperty.AddOwner( typeof( FocusWithinHelper ) );

        public static bool GetIsFocusWithin( DependencyObject elem )
        {
            return (bool)elem.GetValue( IsFocusWithinProperty );
        }
    }

     

    Define a style that uses that. Make sure to map a namespace that points to FocusWithinHelper class that we defined above.

            <Style TargetType="{x:Type ige:XamTextEditor}" >
                <Style.Triggers>
                    <Trigger Property="local:FocusWithinHelper.IsFocusWithin" Value="True" >
                        <Setter Property="BorderBrush" Value="DarkOrange"/>
                        <Setter Property="Background" Value="Black"/>
                        <Setter Property="BorderThickness" Value="4"/>
                    </Trigger>
                </Style.Triggers>
            </Style>        

     

     Hope this helps,

    Sandip