routed-commands

Should I check an ICommand's CanExecute method before calling Execute from procedural code?

血红的双手。 提交于 2019-12-05 03:09:35
When using ICommand s in XAML, WPF uses the CanExecute method to enable or disable controls associated with the command. But what if I am calling Execute from procedural code? Should I first check CanExecute to make sure that the command can execute, or should Execute take care of this check for me? In other words, should I do this: if (someCommand.CanExecute(parameter, target)) someCommand.Execute(parameter, target); Or just this: someCommand.Execute(parameter, target); Good style would dictate that you should do the former, check CanExecute first. This will enforce proper decomposition and a

WPF TextBox Intercepting RoutedUICommands

柔情痞子 提交于 2019-12-04 18:05:24
问题 I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I have my own custom functionality implemented using the Command Pattern). It seems, however, that the TextBox control is intercepting my "Undo" RoutedUICommand. What is the simplest way to disable this so that I can catch Ctrl+Z at the root of my UI tree? I would like to avoid putting a ton of code/XAML into each TextBox in my application if possible. The following briefly demonstrates the problem: <Window x:Class

How can I handle WPF routed commands in my ViewModel without code-behind?

馋奶兔 提交于 2019-12-04 08:46:17
问题 According to my understanding of MVVM, it is a good practice to handle routed commands directly in the ViewModel. When a routed command is defined in a ViewModel as a RelayCommand (or DelegateCommand), it is easy to bind directly to the command like this: Command={Binding MyViewModelDefinedCommand}. Actually, for routed command that are defined outside of my ViewModel, I handle those commands in the Code behind of the View and forward calls to the ViewModel. But I find it awkward that I have

In WPF how do I implement ICommandSource to give my custom control ability to use Command from xaml?

徘徊边缘 提交于 2019-12-04 01:47:48
Could you please provide a sample, of how do you implement the ICommandSource interface. As I want my UserControl , which doesn't have the ability to Specify command in xaml, to have this ability. And to be able to handle the command when user clicks on the CustomControl . Here's an example : public partial class MyUserControl : UserControl, ICommandSource { public MyUserControl() { InitializeComponent(); } public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public static readonly DependencyProperty CommandProperty =

WPF TextBox Intercepting RoutedUICommands

青春壹個敷衍的年華 提交于 2019-12-03 11:39:05
I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I have my own custom functionality implemented using the Command Pattern ). It seems, however, that the TextBox control is intercepting my "Undo" RoutedUICommand. What is the simplest way to disable this so that I can catch Ctrl+Z at the root of my UI tree? I would like to avoid putting a ton of code/XAML into each TextBox in my application if possible. The following briefly demonstrates the problem: <Window x:Class="InputBindingSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns

How can I handle WPF routed commands in my ViewModel without code-behind?

偶尔善良 提交于 2019-12-03 03:33:58
According to my understanding of MVVM, it is a good practice to handle routed commands directly in the ViewModel. When a routed command is defined in a ViewModel as a RelayCommand (or DelegateCommand), it is easy to bind directly to the command like this: Command={Binding MyViewModelDefinedCommand}. Actually, for routed command that are defined outside of my ViewModel, I handle those commands in the Code behind of the View and forward calls to the ViewModel. But I find it awkward that I have to do so. It goes against recommended MVVM good practices. I think that there should be a more elegant

Set command target to template part

时光毁灭记忆、已成空白 提交于 2019-12-02 18:18:38
问题 1) Custom DataGrid with CommandBindings. 2) A RoutedCommand Definition. 3) A Command Target Definition. (XAML) CS : //(1) public class CustomDataGrid : DataGrid { this.CommandBindings.Add(new CommandBinding(Commands.ClearInputCommand, ClearFilter, ClearFilterCanExecute)); } //(2) public static class Commands { public static RoutedCommand ClearInputCommand = new RoutedCommand("ClearInputCommand", typeof(Commands)); } XAML : <!-- (3) --> <local:CustomDataGrid x:Name="grid" /> <Button Command="

WPF Routed Command with Bindings per-Tab

我的梦境 提交于 2019-12-01 13:45:00
I intended to disable and enable the Buttons outside the TabControl, just like those inside the TabItem when the current tab is changed. But the CommandBindings of the TabItem do not seem to impact "up" the visual tree. What is the right way to do this? With this XAML: <Window x:Class="WpfApplication10.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication10" Title="MainWindow" Height="350" Width="525"> <StackPanel> <Button Content="MyCommand1" Command="local:MainWindow

WPF Routed Command with Bindings per-Tab

拜拜、爱过 提交于 2019-12-01 10:09:27
问题 I intended to disable and enable the Buttons outside the TabControl, just like those inside the TabItem when the current tab is changed. But the CommandBindings of the TabItem do not seem to impact "up" the visual tree. What is the right way to do this? With this XAML: <Window x:Class="WpfApplication10.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication10" Title=

RoutedCommands Executed and PreviewExecuted events

别来无恙 提交于 2019-12-01 05:38:52
My problem is that I would like to handle a commands in multiple places. For instance I have my custom UserControl where a Button is bound to some command. I have a command binding in that control but I also have a command binding in a Window which uses this control. My aim is to perform some action inside the control while not interrupting handling of the command in the Window. I tried experimenting with Executed and PreviewExecuted events but with no luck. Then I simulated the problem in a single window (code posted below). <Window x:Class="CommandingEvents.Window1" xmlns="http://schemas