c# wpf mvvm textbox paste

c# - Wpf MVVM Cómo manejar TextBox "pegar evento" en el ViewModel



paste (2)

También he estado luchando con este tipo de problema en los últimos días. Mi primer enfoque sería tener una propiedad en la máquina virtual que está vinculada al cuadro de texto (que estoy seguro que ya tiene). A continuación, vincula un ICommand a un evento para gestionar el evento on paste:

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>

necesita definir el espacio de nombres en la parte correcta del código XAML y luego colocar los desencadenadores de interacción como parte de la definición del cuadro de texto. Aquí estoy capturando el evento RowEditEnding para hacer algo similar a lo que estás intentando.

El enlace del comando es otra pieza, avíseme si necesita más información sobre cómo debe configurarse.

Desarrollo una aplicación con el uso del patrón MVVM . Estoy usando la biblioteca MVVMLight para hacer esto. Entonces, si necesito manejar el evento TextBox TextChange , escribo en XAML:

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

donde PropertyGridTextChange es el Command en ViewModel . ¡Pero TextBox no tiene evento de Paste !

Esta solución solo funciona si la aplicación no usa el patrón MVVM , porque necesita tener un enlace en TextBox .

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

Detalles importantes: TextBox ubicado dentro de DataTemplate . No tengo idea de cómo puedo manejar "pegar evento". Quiero que se invoca PasteCommand cuando PasteCommand texto en TextBox . Y necesito que TextBox.Text o TextBox se pase como parámetro en PasteCommandMethod .

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


Puedo sugerirle que responda mi pregunta.

Ayudante de clase.

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); } }

Usando en XAML. En TextBox como atributo.

TextBoxPasteBehavior.PasteCommand="{Binding PropertyGridTextPasted}"

PropertyGridTextPasted - Comando en ViewModel .