3

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

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

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

提前致谢

4

2 回答 2

2

我喜欢使用附加行为。这是我在获得焦点时在文本框中选择值的示例。这样您就可以将此行为应用于任何文本框。附加行为的一大优点是许多属性/事件位于 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" 
于 2013-02-26T19:52:13.153 回答
0

如果您使用的是 Caliburn,您可以执行以下操作来触发 ViewModel 上的某些内容:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="GotFocus">
        <cal:ActionMessage MethodName="openKeyboard" />
    </i:EventTrigger>
</i:Interaction.Triggers>

然后在 ViewModel 上创建一个与MethodName同名的方法:

public void openKeyboard()
{
    // Do your stuff
}

我希望它有所帮助。

于 2019-10-31T09:47:43.123 回答