В MainView у меня есть кнопка, которая вызывает команду (мы назовем ее «Выполнить»), хранящуюся в MainViewModel. Я также хочу, чтобы эта команда вызывалась при нажатии левой кнопки мыши (фактически, только при нажатии кнопки «Вверх»), но только тогда, когда отмечен флажок MouseUpCheckBox.
Когда у меня был весь код в представлении, я выполнил статическую привязку для Execute, но затем использовал привязку ElementName к окну (с именем w_window) для IsChecked и проверил значение свойства в обработчик MouseLeftButtonUpEvent.
XAML
Код: Выделить всё
Execute
Execute on Mouse Up
Код: Выделить всё
public MainView()
{
InitializeComponent();
this.CommandBindings.Add(new CommandBinding(Execute, ExecuteExecuted));
MyDesigner.AddHandler(UIElement.MouseLeftButtonUpEvent, new RoutedEventHandler((o, eventArgs) =>
{
if (ExecuteOnMouseUp)
{
Execute.Execute(null, this);
}
}), true);
}
#region ExecuteOnMouseUp
///
/// ExecuteOnMouseUp Dependency Property
///
public static readonly DependencyProperty ExecuteOnMouseUpProperty =
DependencyProperty.Register("ExecuteOnMouseUp", typeof(bool), typeof(MainView),
new FrameworkPropertyMetadata((bool)false));
///
/// Gets or sets the ExecuteOnMouseUp property. This dependency property
/// indicates ....
///
public bool ExecuteOnMouseUp
{
get { return (bool)GetValue(ExecuteOnMouseUpProperty); }
set { SetValue(ExecuteOnMouseUpProperty, value); }
}
#endregion
#region Execute
///
/// The Execute command ....
///
public static RoutedUICommand Execute
= new RoutedUICommand("Execute", "Execute", typeof(MainView));
private void ExecuteExecuted(object sender, ExecutedRoutedEventArgs e)
{
...
}
#endregion
Подробнее здесь: https://stackoverflow.com/questions/389 ... a-mvvm-way
Мобильная версия