С#. ВПФ. Назначает функцию объекту данных. Вставка в текстовое поле с сохранением шаблона MVVM.C#

Место общения программистов C#
Гость
С#. ВПФ. Назначает функцию объекту данных. Вставка в текстовое поле с сохранением шаблона MVVM.

Сообщение Гость »


To the DataObject.Pasting event in the Textbox I want to assign the TextBoxPasting function which is located in the viewmodel (MVVM pattern). Unfortunately the code does not work. I use the library:
xmlns:behaviours="http://schemas.microsoft.com/xaml/behaviors".
View - code: ViewModel - code:

Код: Выделить всё

public class MechanicViewModel : ViewModelBase, IMechanicViewModel
{
private static readonly Regex _regex = new Regex("[^0-9.-]+");

public MechanicViewModel()
{
DataObjectPastingCommand = new DelegateCommand(TextBoxPasting);
}

public DelegateCommand DataObjectPastingCommand { get; private set; }

private static bool IsTextAllowed(string text)
{
return !_regex.IsMatch(text);
}

private void TextBoxPasting(DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
string text = (string)e.DataObject.GetData(typeof(string));
if (!IsTextAllowed(text))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
}


Источник: https://stackoverflow.com/questions/781 ... ving-the-m

Вернуться в «C#»