2015-02-05 132 views
2

我使用MVVM模式开发应用程序。我使用MVVMLight库来做到这一点。所以,如果我需要处理TextBoxTextChange事件我在XAML写:Wpf MVVM如何在ViewModel中处理TextBox“粘贴事件”

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

其中PropertyGridTextChangeViewModelCommand。但TextBox没有Paste活动!如果应用程序不使用MVVM模式,因为你需要有TextBox链接

This解决方案仅适用。

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

重要的细节 - TextBox放置在DataTemplate。 我不知道如何处理“粘贴事件”。 当我将文本粘贴到TextBox时,我想要调用PasteCommand。我需要TextBox.TextTextBox本身作为参数传递到PasteCommandMethod

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

private void PasteCommandMethod(Object obj) 
{ 
} 
+0

如果粘贴文本,您希望视图模型做什么? – Dirk 2015-02-05 14:40:28

+0

我想要一些'Command'被调用。 – monstr 2015-02-05 14:42:43

+0

好的,我应该更准确地说出这个问题:为什么你特别想处理复制和粘贴事件,而不是仅仅绑定Text属性的数据? – Dirk 2015-02-05 14:45:29

回答

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> 

你需要在XAML代码的适当部分来定义命名空间,然后把交互文本框定义的一部分触发英寸在这里,我捕获了RowEditEnding事件来做一些与你正在尝试的东西类似的东西。

命令绑定是另一件事,让我知道如果你需要更多的信息,需要如何设置。

+0

我意识到这可能不适用于数据模板。 。 。 – 2015-02-05 15:04:44

+0

是否意味着'作为'DataGrid'定义'的一部分?因为'TextBox'没有'RowEditEditing'事件。 – monstr 2015-02-05 15:17:55

+0

@monstr是的,我做到了。 。 。只是展示我如何实施。但是答案没有帮助,因为我误解了他的问题。 。 。我想,在咖啡之前,我错过了你想在数据模板中做这件事的部分。 。 。我不能说话,因为我还没有尝试过。 – 2015-02-05 16:29:58

5

我可以建议对我的问题的答案。

助手。

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

+0

非常感谢。它帮助了我。 3为这个解决方案欢呼。 – 2017-03-08 08:58:24