首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Enter键按下时绑定TextBox?

在Enter键按下时绑定TextBox?
EN

Stack Overflow用户
提问于 2018-03-22 02:28:48
回答 2查看 0关注 0票数 0

TextBox上的默认数据绑定是TwoWay,它仅在TextBox失去焦点时才将文本提交给属性。 当我按下TextBox上的Enter键时,是否有任何简单的XAML方法可以使数据绑定发生? 我知道在后面的代码中很容易做到,但如果这个TextBox是在一些复杂的DataTemplate中怎么做?

EN

回答 2

Stack Overflow用户

发布于 2018-03-22 10:32:50

您以通过创建一个attached behaviour.来使成为XAML方法。

像这样

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();
        }
    }
}

然后在你的XAML中,将该InputBindingsManager.UpdatePropertySourceWhenEnterPressedProperty属性设置为你要在Enter按下该键时更新的属性。如何

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

Stack Overflow用户

发布于 2018-03-22 12:22:07

可以设置一个绑定,以便通过设置UpdateSourceTrigger属性来更新每当TextBox中的文本发生更改(而不是当TextBox失去焦点时)的绑定,如下所示:

<TextBox Name="itemNameTextBox"
    Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}" />

如果将UpdateSourceTrigger设置为“Explicit”,然后处理TextBox的PreviewKeyDown事件(查找Enter键),那么可以实你您想要的,但这需要代码隐藏。也许某种附加属性(类似于EnterKeyTraversal属性)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档