Textbox in ItemTemplate of ListBox gets focus but doesn't react to keyboard input

Hello,

I have a dual ListBox that uses an ItemTemplate. Drag and drop is enabled.

If I put a sftextbox in the ItemTemplate it happens that the texbox is able to get focus but the value of the textbox doesn't change when keyboard is used. If i make right-click and paste then value of the textbox is updated with the clipboard value.

I managed to make the textbox editable by setting in ListBoxEvents:

ListBoxItemKeyDown="@((e)=>{e.PreventDefaultAction=false;})"

but the first key that is pressed on the keyboard is ignored. The textbox is only updated from the second key pressed onwards, skipping the first key.

Could you please check this behaviour? Or is the ListBox not intended to edit data?

Thank you,

Andrea


3 Replies

BA Bem Addun July 6, 2024 01:41 PM UTC

When a TextBox is placed inside the ItemTemplate of a ListBox, it can receive focus when clicked, but may not respond to keyboard input. This is because the ListBox is designed to handle keyboard navigation and selection by default.To ensure the TextBox inside the ItemTemplate receives keyboard input when focused:

  1. Set the IsTabStop property of the TextBox to true to allow it to receive focus.
  2. Handle the PreviewLostFocus event of the TextBox and mark the event as handled to prevent the ListBox from stealing focus.
  3. Use a Style trigger to set the FocusManager.FocusedElement property of the TextBox when certain conditions are met.

Here's an example of how to set up the TextBox inside the ItemTemplate to receive keyboard input:

xaml
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276" SelectionChanged="lstEmails_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Label Visibility="Hidden" Content="{Binding ID}"></Label> <TextBox Width="200" Text="{Binding EmailAddress}" IsTabStop="True" PreviewLostFocus="TextBox_PreviewLostFocus"> <TextBox.Style> <Style> <Style.Triggers> <DataTrigger Binding="{Binding SomeCondition}" Value="True"> <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=TextBoxInsideListBoxItemTemplate}"/> </DataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

In this example:

  • The TextBox has IsTabStop="True" to allow it to receive focus.
  • The PreviewLostFocus event is handled to prevent the ListBox from stealing focus.
  • A Style trigger sets the FocusManager.FocusedElement property of the TextBox when a certain condition is met.

By following these steps, the TextBox inside the ItemTemplate should receive keyboard input when focused.



Temu, an online shopping platform, is offering significant discounts through two prominent coupon codes: ACT200019 and ACP856709. 

  1. Download the Temu app, available for both iPhone and Android devices.

  2. Browse through the wide range of products available on the platform.

  3. Add desired items to your cart.

  4. During the checkout process, enter either ACT200019 or ACP856709 in the coupon code field.

  5. Apply the code to see the discount reflected in your total.



IN info replied to Bem Addun July 6, 2024 02:19 PM UTC

Thanks Bem Addun but our project is in blazor (server version) and not in WPF.



KV Keerthikaran Venkatachalam Syncfusion Team July 8, 2024 05:39 PM UTC

Hi info,


Thank you for reaching out to Syncfusion Support.


We have checked the reported query, and when integrating text fields such as SfTextBox or SfNumericBox within a SfListBox component, it is essential to manage keyboard interactions properly to ensure a seamless user experience. A common issue arises when the first key press is not recognized due to the default action of the SfListBox not being cancelled. To resolve this issue, you can prevent the default list box event when editing text fields and re-enable it once editing is complete. Please refer to the code snippets and samples provided below.

<div id="listbox1">

    <h4>Group A</h4>

    <SfListBox TValue="string[]" DataSource="@GroupA" Height="250px" Scope="scope2" TItem="ListData" @attributes="listbox1Attr">

        <ListBoxFieldSettings Text="Text"></ListBoxFieldSettings>

        <ListBoxToolbarSettings Items="@Items"></ListBoxToolbarSettings>

        <ListBoxTemplates TItem="ListData">

            <ItemTemplate>

                <SfTextBox Placeholder=@((context as ListData).Text) @onfocusin="@keyPress" @onfocusout="@onBlur"></SfTextBox>

            </ItemTemplate>

        </ListBoxTemplates>

        <ListBoxEvents TValue="string[]" TItem="ListData" OnActionBegin="OnActionBegin" ListBoxItemKeyDown="ListBoxItemKeyDown"></ListBoxEvents>

    </SfListBox>

</div>

<div id="listbox2">

    <h4>Group B</h4>

    <SfListBox TValue="string[]" Scope="scope1" DataSource="@GroupB" Height="250px" TItem="ListData" @attributes="listbox2Attr">

        <ListBoxFieldSettings Text="Name"></ListBoxFieldSettings>

        <ListBoxTemplates TItem="ListData">

            <ItemTemplate>

                <SfTextBox Placeholder=@((context as ListData).Text) @onfocusin="@keyPress" @onfocusout="@onBlur"></SfTextBox>

            </ItemTemplate>

        </ListBoxTemplates>

        <ListBoxEvents TValue="string[]" TItem="ListData" OnActionBegin="OnActionBegin" ListBoxItemKeyDown="ListBoxItemKeyDown"></ListBoxEvents>

    </SfListBox>

</div>

….

public void OnActionBegin(Syncfusion.Blazor.DropDowns.ActionBeginEventArgs args)

    {

        args.Cancel = isEditing;

    }

private bool isEditing = false;

 

    private void keyPress()

    {

        isEditing = true;

    }

 

    private void onBlur()

    {

        isEditing = false;

    }

 

    private void ListBoxItemKeyDown(ListBoxKeyDownEventArgs args)

    {

        args.PreventDefaultAction = !isEditing;

    }


Sample link: https://blazorplayground.syncfusion.com/LDhzjwtPRUplBXQF


Please let us know if you need any further assistance on this.


Regards,

KeerthiKaran K V


Loader.
Up arrow icon