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.
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: