Wpf MVVM如何在ViewModel中处理TextBox"粘贴事件"

mon*_*str 2 c# wpf textbox paste mvvm

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

<I:EventTrigger EventName="TextChanged">
    <I:InvokeCommandAction Command="{Binding PropertyGridTextChange}"/>
</I:EventTrigger>
Run Code Online (Sandbox Code Playgroud)

哪里PropertyGridTextChangeCommandViewModel.但TextBox没有Paste事件!

解决方案仅在应用程序不使用MVVM模式时才有效,因为您需要链接TextBox.

<DataTemplate x:Key="StringTemplate">
    <TextBox Text="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    </TextBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

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

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

private void PasteCommandMethod(Object obj)
{
} 
Run Code Online (Sandbox Code Playgroud)

mon*_*str 6

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

类帮手.

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);
}
}
Run Code Online (Sandbox Code Playgroud)

在XAML中使用.TextBox属性中.

TextBoxPasteBehavior.PasteCommand="{Binding PropertyGridTextPasted}"
Run Code Online (Sandbox Code Playgroud)

PropertyGridTextPasted- 命令在ViewModel.


Pau*_*son 0

我最近几天也一直在为此类问题而苦苦挣扎。我的第一个方法是在虚拟机中拥有一个绑定到文本框的属性(我确信您已经拥有了)。然后将 ICommand 绑定到事件以处理粘贴事件:

            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="RowEditEnding">
                <i:InvokeCommandAction Command="{Binding DocRowEdit}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

您需要在 XAML 代码的适当部分定义命名空间,然后将交互触发器作为文本框定义的一部分放入。在这里,我捕获 RowEditEnding 事件来执行一些类似于您正在尝试的操作。

命令绑定是另一部分,如果您需要有关如何设置的更多信息,请告诉我。