Pass command parameter to method in ViewModel in WPF?

“ViewModel” implies MVVM. If you’re doing MVVM you shouldn’t be passing views into your view models. Typically you do something like this in your XAML: <Button Content=”Edit” Command=”{Binding EditCommand}” CommandParameter=”{Binding ViewModelItem}” > And then this in your view model: private ViewModelItemType _ViewModelItem; public ViewModelItemType ViewModelItem { get { return this._ViewModelItem; } set { this._ViewModelItem = … Read more

CommandManager.InvalidateRequerySuggested() isn’t fast enough. What can I do?

CommandManager.InvalidateRequerySuggested() tries to validate all commands, which is totally ineffective (and in your case slow) – on every change, you are asking every command to recheck its CanExecute()! You’d need the command to know on which objects and properties is its CanExecute dependent, and suggest requery only when they change. That way, if you change … Read more

What is the actual task of CanExecuteChanged and CommandManager.RequerySuggested?

CanExecuteChanged notifies any command sources (like a Button or MenuItem) that are bound to that ICommand that the value returned by CanExecute has changed. Command sources care about this because they generally need to update their status accordingly (eg. a Button will disable itself if CanExecute() returns false). The CommandManager.RequerySuggested event is raised whenever the … Read more

Is Josh Smith’s implementation of the RelayCommand flawed?

I’ve found the answer in Josh’s comment on his “Understanding Routed Commands” article: […] you have to use the WeakEvent pattern in your CanExecuteChanged event. This is because visual elements will hook that event, and since the command object might never be garbage collected until the app shuts down, there is a very real potential … Read more

ICommand vs RoutedCommand

As you have noticed the RoutedCommand class is an implementation of the ICommand interface, its main distinction if that its function is similar to that of a RoutedEvent: The Execute and CanExecute methods on a RoutedCommand do not contain the application logic for the command as is the case with a typical ICommand, but rather, … Read more

ICommand MVVM implementation

This is almost identical to how Karl Shifflet demonstrated a RelayCommand, where Execute fires a predetermined Action<T>. A top-notch solution, if you ask me. public class RelayCommand : ICommand { private readonly Predicate<object> _canExecute; private readonly Action<object> _execute; public RelayCommand(Predicate<object> canExecute, Action<object> execute) { _canExecute = canExecute; _execute = execute; } public event EventHandler CanExecuteChanged … Read more

techhipbettruvabetnorabahisbahis forumueduedueduseduseduedusedusedueduedu