Bind Textbox on Enter-Key Press

Bind TextBox on Enter-key press

You can make yourself a pure XAML approach by creating an attached behaviour.

Something like this:

public static class InputBindingsManager
{

public static readonly DependencyProperty UpdatePropertySourceWhenEnterPressedProperty = DependencyProperty.RegisterAttached(
"UpdatePropertySourceWhenEnterPressed", typeof(DependencyProperty), typeof(InputBindingsManager), new PropertyMetadata(null, OnUpdatePropertySourceWhenEnterPressedPropertyChanged));

static InputBindingsManager()
{

}

public static void SetUpdatePropertySourceWhenEnterPressed(DependencyObject dp, DependencyProperty value)
{
dp.SetValue(UpdatePropertySourceWhenEnterPressedProperty, value);
}

public static DependencyProperty GetUpdatePropertySourceWhenEnterPressed(DependencyObject dp)
{
return (DependencyProperty)dp.GetValue(UpdatePropertySourceWhenEnterPressedProperty);
}

private static void OnUpdatePropertySourceWhenEnterPressedPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
UIElement element = dp as UIElement;

if (element == null)
{
return;
}

if (e.OldValue != null)
{
element.PreviewKeyDown -= HandlePreviewKeyDown;
}

if (e.NewValue != null)
{
element.PreviewKeyDown += new KeyEventHandler(HandlePreviewKeyDown);
}
}

static void HandlePreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
DoUpdateSource(e.Source);
}
}

static void DoUpdateSource(object source)
{
DependencyProperty property =
GetUpdatePropertySourceWhenEnterPressed(source as DependencyObject);

if (property == null)
{
return;
}

UIElement elt = source as UIElement;

if (elt == null)
{
return;
}

BindingExpression binding = BindingOperations.GetBindingExpression(elt, property);

if (binding != null)
{
binding.UpdateSource();
}
}
}

Then in your XAML you set the InputBindingsManager.UpdatePropertySourceWhenEnterPressedProperty property to the one you want updating when the Enter key is pressed. Like this

<TextBox Name="itemNameTextBox"
Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}"
b:InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text"/>

(You just need to make sure to include an xmlns clr-namespace reference for "b" in the root element of your XAML file pointing to which ever namespace you put the InputBindingsManager in).

Bind textbox to 'enter' key

$("#post").focus(function() {
$(this).data("hasfocus", true);
});

$("#post").blur(function() {
$(this).data("hasfocus", false);
});

$(document.body).keyup(function(ev) {
// 13 is ENTER
if (ev.which === 13 && $("#post").data("hasfocus")) {
...
}
});

I recommend you instead bind the the ENTER keyup event on your $("#post") input directly rather then listening for the event on the entire page.

Bind Return key press to text box only while focused

This is the default behaviour. If I put the following two TextBoxes in a StackPanel, give the keyboard focus to the second one and press ENTER, the command is not fired.

<StackPanel>
<TextBox Text="{Binding SearchBoxNumber, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding SearchCommand}" />
</TextBox.InputBindings>
</TextBox>

<TextBox />
</StackPanel>

Command for WPF TextBox that fires up when we hit Enter Key

Aryan, not every WPF object supports commanding. So if you wan't to do that you'll need either to call your view model from your code behind (a little coupled) or use some MVVM Messaging implementation to decouple that. See MVVM Light Messaging toolkit for an example. Or simple use triggers like this:

<TextBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeDataCommand Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>

Capturing the Enter key in a TextBox

You need to change the UpdateSourceTrigger on your TextBox.Text binding to PropertyChanged. See here.

Binding textbox enter press to reactive command

Proper syntax for RxUI command:

    public ReactiveCommand<object> EnterCmd { get; private set; }
ObservableAsPropertyHelper<bool> _isLoading;
public bool IsLoading { get { return _isLoading.Value; } }

public MainViewModel()
{
EnterCmd = ReactiveCommand.Create(
this.WhenAny(x => x.UserID, x => x.Value)
.Select(x => !String.IsNullOrWhiteSpace(x)));

EnterCmd.IsExecuting.ToProperty(this, x => x.IsLoading, out _isLoading);

EnterCmd.Subscribe(x =>
{
Console.WriteLine(x);
});

ReactiveCommand is a static helper class (factory methods mostly), the real type is the generic version of it.

Submit a textbox by pressing enter key

See this: Bind textbox to 'enter' key
and form.submit() method.



Related Topics



Leave a reply



Submit