RadWatermarkTextBox and EventToCommandBehavior

0 Answers 59 Views
WatermarkTextBox
Neil N
Top achievements
Rank 2
Iron
Iron
Veteran
Neil N asked on 14 Jul 2024, 03:36 PM

I'm trying to capture when the Enter key is pressed in a RadWatermarkTextBox without adding code to code-behind.

In the XAML I have:


            <telerik:RadWatermarkTextBox MinHeight="26"
                                         x:Name="searchCompleteBox"
                                         VerticalAlignment="Center"
                                         Text="{Binding SearchText, Mode=TwoWay}"
                                         Width="200" 
                                         WatermarkContent="Enter name, address, or ID">
                <telerik:EventToCommandBehavior.EventBindings>
                    <telerik:EventBinding Command="{Binding OnSearchCompleteBoxKeyDownCommand}"
                                     EventName="KeyDown"
                                     PassEventArgsToCommand="True" />
                </telerik:EventToCommandBehavior.EventBindings>
            </telerik:RadWatermarkTextBox>

In the ViewModel:


    private async void OnSearchCompleteBoxKeyDown(System.Windows.Input.KeyEventArgs e)
    {        
        if (e.Key == Key.Enter)
        {
            await SearchForMembersAsync();
        }
    }

The problem is SearchText is not updating. So if I type something like "smith" OnSearchCompleteBoxKeyDown fires five times, the if result is false five times, but SearchText contains an empty string instead of "smith".

SearchText is declared properly and is updated properly if I take the EventToCommandBehavior code out.  This all works properly if I use code-behind but I want to avoid that.

Dimitar
Telerik team
commented on 15 Jul 2024, 01:10 PM

Hi Neil, 

This happens because the property is updated when the text box loses focus by default. You need to set the UpdateSourceTrigger. For example:

  Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

No answers yet. Maybe you can help?

Tags
WatermarkTextBox
Asked by
Neil N
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or