Wpf MVVM 如何处理TextBox“粘贴事件"在视图模型中 [英] Wpf MVVM How to handle TextBox "paste event" in the ViewModel

查看:78
本文介绍了Wpf MVVM 如何处理TextBox“粘贴事件"在视图模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MVVM 模式开发应用程序.我使用 MVVMLight 库来做到这一点.所以如果我需要处理 TextBox TextChange 事件,我用 XAML 编写:

I develop application with using MVVM pattern. I using MVVMLight library to do this. So if I need to handle TextBox TextChange event I write in XAML:

<I:EventTrigger EventName="TextChanged">
    <I:InvokeCommandAction Command="{Binding PropertyGridTextChange}"/>
</I:EventTrigger>

其中 PropertyGridTextChangeViewModel 中的 Command.但是 TextBox 没有 Paste 事件!

where PropertyGridTextChange is Command in ViewModel. But TextBox has no Paste event!

解决方案仅适用于应用程序不使用 MVVM 模式的情况,因为您需要在 TextBox 上添加链接.

This solution only works if application don't use MVVM pattern, because you need to have link on TextBox.

<DataTemplate x:Key="StringTemplate">
    <TextBox Text="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    </TextBox>
</DataTemplate>

重要细节 - TextBox 放置在 DataTemplate 中.我不知道如何处理粘贴事件".我希望在将文本粘贴到 TextBox 时调用 PasteCommand.我需要将 TextBox.TextTextBox 本身作为参数传递给 PasteCommandMethod.

Important detail - TextBox placed within DataTemplate. I have no idea how can I handle "paste event". I want PasteCommand to be invoked when I paste text into TextBox. And I need that TextBox.Text or TextBox itself to be passed as parameter into PasteCommandMethod.

private RelayCommand<Object> _pasteCommand;
public RelayCommand<Object> PasteCommand
{
    get
    {
        return _pasteCommand ?? (_pasteCommand =
            new RelayCommand<Object>(PasteCommandMethod));
    }
}

private void PasteCommandMethod(Object obj)
{
} 

推荐答案

我可以建议我的问题的答案.

I can suggest answer on my question.

班级助手.

public class TextBoxPasteBehavior 
{
public static readonly DependencyProperty PasteCommandProperty =
    DependencyProperty.RegisterAttached(
        "PasteCommand",
        typeof(ICommand),
        typeof(TextBoxPasteBehavior),
        new FrameworkPropertyMetadata(PasteCommandChanged)
    );

public static ICommand GetPasteCommand(DependencyObject target)
{
    return (ICommand)target.GetValue(PasteCommandProperty);
}

public static void SetPasteCommand(DependencyObject target, ICommand value)
{
    target.SetValue(PasteCommandProperty, value);
}

static void PasteCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var textBox = (TextBox)sender;
    var newValue = (ICommand)e.NewValue;

    if (newValue != null)
        textBox.AddHandler(CommandManager.ExecutedEvent, new RoutedEventHandler(CommandExecuted), true);
    else
        textBox.RemoveHandler(CommandManager.ExecutedEvent, new RoutedEventHandler(CommandExecuted));

}

static void CommandExecuted(object sender, RoutedEventArgs e)
{
    if (((ExecutedRoutedEventArgs)e).Command != ApplicationCommands.Paste) return;

    var textBox = (TextBox)sender;
    var command = GetPasteCommand(textBox);

    if (command.CanExecute(null))
        command.Execute(textBox);
}
}

在 XAML 中使用.TextBox 中作为属性.

TextBoxPasteBehavior.PasteCommand="{Binding PropertyGridTextPasted}"

PropertyGridTextPasted - ViewModel 中的命令.

这篇关于Wpf MVVM 如何处理TextBox“粘贴事件"在视图模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆