Bağlama-Enter tuşuna basın TextBox
TextBox varsayılan veri bağlama TwoWay TextBox odağını kaybetmiş tek bir özellik için metin kaydeder.
Herhangi bir XAML kolay yolu, veri bağlama bastığımda gerçekleşmesi için anahtar TextBox? Girin. Çok kolay kod arkasında ne olduğunu biliyorum, ama eğer TextBox bu kompleks içinde olduğunu hayal edin DataTemplate.
CEVAP
Kendini saf XAML bir yaklaşım attached behaviour Bir oluşturarak yapabilirsiniz.
Şöyle bir şey:
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();
}
}
}
Eğer XAML zaman güncellemek istediğinize InputBindingsManager.UpdatePropertySourceWhenEnterPressedProperty özelliği ayarladıktan sonraGirintuşa basıldığında. Bu gibi
<TextBox Name="itemNameTextBox"
Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}"
b:InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text"/>
(Sadece "b" XAML dosyanızı daha önce ad olan işaret kök öğesi İnputBindingsManager sizi). bir xmlns clr-ad referans dahil etmek için emin olmak gerekir

'Ding' ne zaman Enter tuÅŸuna...
Düğmesini kapatın sanal klavye tuşuna ...
İçinde tuşuna basın dinleyin .NET uygu...
Tuşuna basın girin olay JavaScript...
Enter tuşuna belirleyen bir Metin bası...