We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to handle focus events of the RibbonTextBox in WPF Ribbon control?

Platform: WPF |
Control: Ribbon |
Published Date: May 6, 2015 |
Last Revised Date: April 13, 2021

You can handle the focus events by using the GotKeyboardFocus and PreviewMouseLeftButtonDown events of RibbonTextBox in WPF Ribbon control. In the following example, the Focus of the RibbonTextBox has been changed similar to the search bar of the Internet Explorer. That means, when the TextBox is selected, the text gets highlighted and on the second click you can enter the text.

MainWindow.Xaml

<syncfusion:RibbonWindow x:Class="Ribbon_131602.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:Ribbon_131602"
        syncfusion:SkinStorage.VisualStyle="Metro"
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <syncfusion:Ribbon Name="MyRibbon" BackStageCornerImageVisibility="Collapsed"
                        EnableMoreCommands="False">            
            <syncfusion:RibbonTab Caption="TextFocus" syncfusion:Ribbon.KeyTip="E">
                <syncfusion:RibbonBar Header="FocusBar">
                    <syncfusion:RibbonTextBox Text="Simple text" x:Name="Text1" />
                    <syncfusion:RibbonTextBox Text="Focus Text" x:Name="Text2" GotKeyboardFocus="Text2_GotKeyboardFocus" PreviewMouseLeftButtonDown="Text2_PreviewMouseLeftButtonDown"/>
                    <syncfusion:RibbonTextBox Text="Simple text" x:Name="Text3" />
                </syncfusion:RibbonBar>
            </syncfusion:RibbonTab>
        </syncfusion:Ribbon>
        <Grid Grid.Row="1">
            <RichTextBox Margin="12">
                <FlowDocument>
                    <Paragraph>
                        In this sample, we have added 3 RibbonTextBox to the TextFocus RibbonTab. The second TextBox has the Text to be selected when it receives focus.
                    </Paragraph> 
                </FlowDocument>
            </RichTextBox>
        </Grid>
    </Grid>
</syncfusion:RibbonWindow>

 

MainWindow.Xaml.cs

public partial class MainWindow : RibbonWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Text2_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        if (Text2 != null && !string.IsNullOrEmpty(Text2.Text))
           Text2.SelectAll();
    }
    private void Text2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(Text2!=null)
        {
           if(!Text2.IsKeyboardFocusWithin)
           {
              e.Handled = true;
              Text2.Focus();
           }
        }
    }
}

The following screenshot displays the RibbonTextBox with focus.

Handle the focus in WPF Ribbon control

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon