简体   繁体   English

MVVM捕获文本框粘贴事件

[英]MVVM catch textbox paste event

I have tried to transform this this (from old application) to MVVM pattern but I don't see how I will do it. 我试图将其(从旧的应用程序)转换为MVVM模式,但我不知道如何实现。

textBoxLoader.AddHandler(CommandManager.ExecutedEvent, new RoutedEventHandler(PasteFunction), true);

    private void PasteFunction(object sender, RoutedEventArgs e)
        {       
            if ((e as ExecutedRoutedEventArgs).Command == ApplicationCommands.Paste)
            {             
                // verify that the textbox handled the paste command

                textBoxLoader.IsEnabled = false;
                List<string[]> MachineList = new List<string[]>();
                List<string> list = new List<string>(Regex.Split(textBoxLoader.Text, Environment.NewLine));
}}

How can I reproduce this and call command from WPF TextBox Paste event ? 如何重现此问题并从WPF TextBox Paste事件调用命令? I can successfully bind Enter Key event , but how do I bind Paste event ? 我可以成功绑定Enter Enter事件 ,但是如何绑定粘贴事件

Below is a code snippet on how I am binding a Icommand in new MVVM ( Enter Key listener ) 以下是有关如何在新的MVVM中绑定Icommand的代码段( Enter键侦听器

<UserControl.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding ClickCommand}" CommandParameter="{Binding Text, ElementName=textBoxLoader}"/>
    </UserControl.InputBindings>

In view : 鉴于:

<TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="120" Height="200"/>

In view model : 在视图模型中:

class MainWindowViewModel : BindableBase
{
    private string text;

    public string Text
    {
        get { return text; }
        set { SetProperty(ref text, value); }
    }
}

In this way, when you paste the text or press Ctrl+V the Text value is updated and PropertyChanged event is raised. 这样,当您粘贴文本或按Ctrl + V时 ,“文本”值将更新,并引发PropertyChanged事件。 Thus you can identify the pasted text into textbox. 因此,您可以识别粘贴到文本框中的文本。

If you want to identify Ctrl+V for some special reason, try this : 如果出于某些特殊原因要标识Ctrl + V,请尝试以下操作:

void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.V)
    {
          // Your action
    }
}

In MVVM the textbox is bound to a property on the VM. 在MVVM中,文本框绑定到VM上的属性。 Why not simply subscribe to the VM's notify property change ( could be done on the VM itself actually ) and look for a change reported on that bound property. 为什么不简单地订阅VM的notify属性更改( 实际上可以在VM本身上完成 )并查找该绑定属性上报告的更改。

Once a change notification is sent, do the logic needed. 发送变更通知后,请执行所需的逻辑。

Example

This working example, wouldn't be used in the real world because the work would be done in the setter of Name , demonstrates how to subscribe to the notify event. 这个工作示例不会在现实世界中使用,因为该工作将在Name的设置器中完成 ,演示了如何订阅notify事件。 When Name is changed the VM catches the change from the event and FullName is computed and the property changed is raised for FullName from the subscription. Name更改虚拟机捕获从事件的变化, FullName是计算和属性改变时,提高了FullName来自订阅。

public class ExampleVM : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; OnPropertyChanged(); }
    }

    public string FullName { get; set; }

    public ExampleVM()
    {
        this.PropertyChanged += (s, a) =>
        {
            if (a.PropertyName == "Name")
            {
                FullName = $"Mr. {Name}";
                OnPropertyChanged("FullName");
            }
        };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

C# 6 coding is used, but can be rewritten in any other version of C#. 使用了C#6编码,但可以在任何其他版本的C#中重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM