wpf - 如何在 mvvm 应用程序中的每个文本框焦点事件上调用事件触发器

标签 wpf mvvm

我正在开发一个触摸屏应用程序,当光标聚焦在任何文本框上时,我需要打开触摸屏键盘。我正在使用以下代码调用我在 View 模型中的命令..

 <i:Interaction.Triggers>
   <i:EventTrigger EventName="GotFocus">
     <i:InvokeCommandAction Command="{Binding openKeyboard}" />
   </i:EventTrigger>
 </i:Interaction.Triggers>

当我在每个文本框上写字时它工作正常......让我有多个单一形式的文本框,有没有什么办法可以通用地写它并且应该适用于我的表单(或)应用程序的所有文本框?

提前致谢

最佳答案

我喜欢使用附加行为。这是我在获得焦点时选择文本框中的值的示例。这样您就可以将此行为应用于任何文本框。附加行为的一大优点是许多属性/事件都在 UIElement 级别,因此您可以跨多个控件重用某些行为。无论如何,这是我的例子:

行为

 public class SelectAllOnFocusedBehavior
{

    private static bool GetSelectAllOnFocused(TextBox textBox)
    {
        return (bool) textBox.GetValue(SelectAllOnFocusedProperty);
    }

    public static void SetSelectAllOnFocused(
        TextBox textBox, bool value)
    {
        textBox.SetValue(SelectAllOnFocusedProperty, value);
    }


    public static readonly DependencyProperty SelectAllOnFocusedProperty =
        DependencyProperty.RegisterAttached(
            "SelectAllOnFocused",
            typeof (bool),
            typeof (SelectAllOnFocusedBehavior),
            new UIPropertyMetadata(false, OnSelectAllOnFocusedChanged));

    private static void OnSelectAllOnFocusedChanged(
        DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TextBox item = depObj as TextBox;
        if (item == null)
            return;
        if (e.NewValue is bool == false)
            return;

        if ((bool) e.NewValue)
        {
            item.PreviewMouseLeftButtonDown += item_IgnoreLeftMouseDown;
            item.GotFocus+=item_GotFocus;
        }
        else
        {
            //remove EventsHere
            item.PreviewMouseLeftButtonDown -= item_IgnoreLeftMouseDown;
            item.GotFocus -= item_GotFocus;
        }


    }

    static void item_IgnoreLeftMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // Find the TextBox
        DependencyObject parent = e.OriginalSource as UIElement;
        while (parent != null && !(parent is TextBox))
            parent = VisualTreeHelper.GetParent(parent);

        if (parent != null)
        {
            var textBox = (TextBox)parent;
            if (!textBox.IsKeyboardFocusWithin)
            {
                // If the text box is not yet focussed, give it the focus and
                // stop further processing of this click event.
                textBox.Focus();
                e.Handled = true;
            }
        }
    }

    static void  item_GotFocus(object sender, RoutedEventArgs e)
    {
         var item = e.OriginalSource as TextBox;
         if (item != null)
            item.SelectAll();
    }


    //EventHandler Here

}

对应wpf

 <TextBox x:Name="blahblah"  
                                 cmds:SelectAllOnFocusedBehavior.SelectAllOnFocused="True"
                                 cmds:NextTabItemOnEnterBehavior.NextTabItemOnEnter="True"
                                 Height="20" Width="75" 

关于wpf - 如何在 mvvm 应用程序中的每个文本框焦点事件上调用事件触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15097620/

相关文章:

.net - MVVM INotifyPropertyChanged 与自动属性名称实现

c# - XAML 元素填充所有剩余空间

wpf - 将 WPF ValidationRule 的状态传递给 MVVM 中的 View 模型

c# - 确定的日期时间还剩 4 分钟时启用按钮

c# - SelectionChanged 显示旧值

c# - 在WPF MVVM中按标签查找标签

c# - .NET - 使用 GifBitmapEncoder 创建循环 .gif

mvvm - 请推荐一本关于 MVVM for silverlight 的完整指南/书籍

WPF 和 MVVM : best architecture for Time-consuming search results?

c# - 如何在后台获取数据并在准备好后用数据异步更新 UI?