Articles in this section
Category / Section

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

2 mins read

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.


Conclusion

I hope you enjoyed learning about how to handle focus events of the RibbonTextBox in WPF Ribbon control.

You can refer to our  WPF Ribbon control feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Ribbon control documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied