How to set focus in custom control with Sftextboxext

Hi,

I want to move the focus to sftextboxext when I click on the Label in a custom control that uses Border and sfTextBoxExt, but it doesn't work.

_textBox?.Focus() in OnLabelPreviewMouseDown is running but focus doesn't move.


<ResourceDictionary

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:controls="clr-namespace:SampleControls.WPF"

    xmlns:syncfusion="http://schemas.syncfusion.com/wpf">


    <ControlTemplate x:Key="LabeledTextBoxTemplate" TargetType="{x:Type controls:LabeledTextBox}">

        <Grid Margin="2">

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="Auto" SharedSizeGroup="LabelSize" />

                <ColumnDefinition />

            </Grid.ColumnDefinitions>


            <Border

                x:Name="PART_Label"

                Grid.Column="0"

                Width="{TemplateBinding TitleWidth}"

                Height="{TemplateBinding TextBoxHeight}"

                Margin="0,0,2,0"

                Background="{DynamicResource LabeledTextBox.Label.Background}"

                BorderBrush="{DynamicResource LabeledTextBox.Border}"

                BorderThickness="1"

                CornerRadius="2"

                SnapsToDevicePixels="true">

                <TextBlock

                    x:Name="labelContent"

                    Margin="2,0,2,0"

                    VerticalAlignment="Center"

                    Background="Transparent"

                    Text="{TemplateBinding Title}" />

            </Border>

            <Border

                x:Name="PART_TextBorder"

                Grid.Column="1"

                Height="{TemplateBinding TextBoxHeight}"

                Background="{DynamicResource LabeledTextBox.Text.Background}"

                BorderBrush="{DynamicResource LabeledTextBox.Border}"

                BorderThickness="2"

                CornerRadius="2">

                <syncfusion:SfTextBoxExt

                    x:Name="PART_TextBox"

                    VerticalAlignment="Center"

                    Background="Transparent"

                    BorderBrush="Transparent"

                    BorderThickness="0"

                    ShowClearButton="{TemplateBinding ShowClearButton}"

                    Watermark="{TemplateBinding Watermark}" />

            </Border>

        </Grid>


        <ControlTemplate.Triggers>

            <Trigger SourceName="labelContent" Property="Text" Value="">

                <Setter TargetName="PART_Label" Property="Padding" Value="0" />

            </Trigger>

            <Trigger Property="IsEnabled" Value="false">

                <Setter TargetName="PART_TextBorder" Property="Opacity" Value="0.56" />

            </Trigger>

            <Trigger Property="IsMouseOver" Value="true">

                <Setter TargetName="PART_TextBorder" Property="BorderBrush" Value="{DynamicResource LabeledTextBox.MouseOver.Border}" />

            </Trigger>

            <Trigger Property="IsKeyboardFocusWithin" Value="true">

                <Setter TargetName="PART_TextBorder" Property="Background" Value="{DynamicResource LabeledTextBox.Focus.Background}" />

                <Setter TargetName="PART_TextBorder" Property="BorderBrush" Value="{DynamicResource LabeledTextBox.Focus.Border}" />

                <Setter TargetName="PART_Label" Property="Background" Value="{DynamicResource LabeledTextBox.Focus.Background}" />

            </Trigger>

        </ControlTemplate.Triggers>

    </ControlTemplate>

</ResourceDictionary>


namespace sampleControls.WPF

{

    [TemplatePart(Name = "PART_Label", Type = typeof(Border))]

    [TemplatePart(Name = "PART_TextBox", Type = typeof(SfTextBoxExt))]

    public class LabeledTextBox : SfTextBoxExt

    {

        private Border _label;

        private SfTextBoxExt _textBox;


        static LabeledTextBox()

        {

            DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledTextBox),

                new FrameworkPropertyMetadata(typeof(LabeledTextBox)));

        }

        public override void OnApplyTemplate()

        {

            base.OnApplyTemplate();

            _label = GetTemplateChild("PART_Label") as Border;

            _textBox = GetTemplateChild("PART_TextBox") as SfTextBoxExt;


            if (_label != null)

                _label.PreviewMouseDown += OnLabelPreviewMouseDown;

        }


        private void OnLabelPreviewMouseDown(object sender, MouseButtonEventArgs e)

        {

            _textBox?.Focus();

        }

    }

}


Attachment: TMControls.WPF_1dd9e61a.zip

3 Replies

AJ AhamedAliNishad JahirHussain Syncfusion Team June 21, 2023 12:15 PM UTC

Hi Yam,

 

We have reviewed your sample and were able to reproduce the reported issue. We have done a workaround for you.

 

In WPF, the UI controls can only be accessed and modified on the UI thread. When you click on the label, the event handler OnLabelPreviewMouseDown is executed on the UI thread. However, there might be some other pending UI operations or events in the queue that are preventing the _textBox.Focus() method from executing immediately.

 

By using the Dispatcher.BeginInvoke method, you are essentially queuing the Focus() method call to be executed asynchronously on the UI thread, allowing any pending operations to complete before focusing the _textBox control. This helps ensure that the focus operation is performed at the appropriate time, avoiding any potential conflicts or interference with other UI-related tasks.

 

By invoking the Focus() method asynchronously using the Dispatcher, you give the UI thread a chance to finish its current tasks and then execute the focus operation separately. This can help resolve issues where the focus is not working as expected due to timing or synchronization problems.

 

Kindly get the modified sample from the attachment and let us know if you need any other details.

 

Regards,

Ahamed Ali Nishad.


Attachment: TMControls.WPF_1dd9e61a_bafa0622.zip


YA Yama June 22, 2023 12:12 AM UTC

Hi Nishad,

Thanks for the quick support.

I was able to get it to work as expected.




PR Preethi Rajakandham Syncfusion Team June 22, 2023 05:02 AM UTC

Hi Yama,

You're welcome. We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help.

Regards,

Preethi R


Loader.
Up arrow icon