【问题标题】:MVVM catch textbox paste eventMVVM 捕获文本框粘贴事件
【发布时间】:2016-06-21 14:21:25
【问题描述】:

我试图将这个(从旧应用程序)转换为 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));
}}

如何从 WPF 文本框 粘贴事件 重现这个和调用命令? 我可以成功绑定Enter Key事件,但是如何绑定Paste事件呢?

下面是关于我如何在新的 MVVM 中绑定 Icommand 的代码 sn-p(Enter Key listener

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

【问题讨论】:

标签: c# wpf mvvm


【解决方案1】:

在视图中:

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

在视图模型中:

class MainWindowViewModel : BindableBase
{
    private string text;

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

这样,当您粘贴文本或按下 Ctrl+V 时,Text 值会更新并引发 PropertyChanged 事件。因此,您可以将粘贴的文本识别到文本框中。

如果你因为某些特殊原因想识别 Ctrl+V,试试这个:

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

【讨论】:

  • 因为它的 mvvm 我应该没有返回代码,如果我使用 previewkeydown 我可以使用我发布在顶部的函数:private void PasteFunction(object sender, RoutedEventArgs e)。我必须使用 Icommand 在视图/视图模型之间进行任何操作并绑定到我
  • 如果您对 propertychanged 的​​事情也不满意,那么试试这样的事情。 stackoverflow.com/questions/28346652/… 你不能有一个空代码来实现你所需要的,除非你单独使用propertychanged。你总是可以在后面的代码中查看相关代码。它不违反 MVVM 模式。不管怎样,试试上面的链接。它有助于将 Paste 命令路由到视图模型。
  • 是的,我意识到我不能在纯 wpf-MV 中对粘贴命令采取行动,但你指出我只是纠正代码只关心 propertyChanged。所以我什至不需要关心“Enter”或“ctrl” +v" .
  • 在我看来,没有必要完全消除回车按键命令。我也使用类似的命令来保存文本和所有内容。唯一的问题是很难实现 Ctrl+v 行为和所有。但如何设计它由您决定。如果确实需要,您甚至可以在后面的代码中处理粘贴的事件并将其路由到视图模型。但既然你需要一个干净的代码,这将是最好的方法。
【解决方案2】:

在 MVVM 中,文本框绑定到 VM 上的属性。为什么不简单地订阅 VM 的通知属性更改(实际上可以在 VM 本身上完成)并查找在该绑定属性上报告的更改。

发送更改通知后,执行所需的逻辑。

示例

这个工作示例,不会在现实世界中使用,因为工作将在 Name 的设置器中完成,演示了如何订阅通知事件。当 Name 更改时,VM 会从事件中捕获更改并计算 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 编码,但可以用任何其他版本的 C# 重写。

【讨论】:

  • 你有这方面的例子吗?
  • @Zwan 查看示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
相关资源
最近更新 更多