delegatecommand

Working with DelegateCommand's CanExecute action

心不动则不痛 提交于 2020-01-15 07:55:12
问题 I've a ViewModel class like this in a Prism / WPF project. public class ContentViewModel : ViewModelBase, IContentViewModel { public ContentViewModel(IPersonService personService) { Person = personService.GetPerson(); SaveCommand = new DelegateCommand(Save, CanSave); } public Person Person { get; set; } public DelegateCommand SaveCommand { get; set; } private void Save() { // Save actions here... } private bool CanSave() { return Person.Error == null; } } The person type used in the above

Prism: have to call RaiseCanExecuteChanged() explicitly

一个人想着一个人 提交于 2020-01-14 03:05:30
问题 Below is a very simple Prism.Wpf example with a DelegateCommand that has both Execute and CanExecute delegates. Suppose that CanExecute depends on some property. It seems that Prism's DelegateCommand doesn't re-evaluate CanExecute condition automatically when this property changes, as RelayCommand does in other MVVM frameworks. Instead, you have to call RaiseCanExecuteChanged() explicitly in the property setter. This leads to a lot of repetitive code in any non-trivial viewmodel. Is there a

WPF: CanExecute is Always Disabled

微笑、不失礼 提交于 2020-01-04 15:31:36
问题 I've got a TextBox for the user to enter a string, and an "Add" button to perform some tasks with the value. I added a CanExecute method to the DelegateCommand so that the button would only work when there was text in the box. However, it's disabled all the time - even when the condition is true. Typing in the box does not enable the button. <TextBox Text="{Binding BuildEntryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <Button Command="{Binding AddBuildCommand}" Content="Add" /

WPF: CanExecute is Always Disabled

北战南征 提交于 2020-01-04 15:31:27
问题 I've got a TextBox for the user to enter a string, and an "Add" button to perform some tasks with the value. I added a CanExecute method to the DelegateCommand so that the button would only work when there was text in the box. However, it's disabled all the time - even when the condition is true. Typing in the box does not enable the button. <TextBox Text="{Binding BuildEntryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <Button Command="{Binding AddBuildCommand}" Content="Add" /

DelegateCommand vs RoutedCommand and gestures - WPF

我们两清 提交于 2020-01-02 09:56:32
问题 is there anyway for DelegateCommand's to support gestures when building a composite WPF app? I'm trying to create a command used by a MenuItem and also a Button, which can be accessed through a keyboard shortcut, but which sits inside a class in a separate assembly. I can use a RoutedCommand, but I want to minimise any code-behind. 回答1: Use InputBindingCollection to map gestures with ICommand. 来源: https://stackoverflow.com/questions/2297321/delegatecommand-vs-routedcommand-and-gestures-wpf

WPF CommandParameter binding and canExecute

半城伤御伤魂 提交于 2020-01-02 03:47:05
问题 I have a template for treeView item: <HierarchicalDataTemplate x:Key="RatesTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=ID}"/> <Button CommandParameter="{Binding Path=ID}" Command="{Binding ElementName=CalcEditView, Path=DataContext.Add}">Add</Button> </StackPanel> </HierarchicalDataTemplate> As a DataContext I have linq entity with ID not null field. The problem is: if I use DelegateCommand 'Add' with CanExecutedMethod: AddRate = new DelegateCommand<int?>

Why use a RelayCommand or DelegateCommand instead of just implementing ICommand?

不羁的心 提交于 2019-12-30 06:27:13
问题 I'm just learning about MVVM in WPF, I'm completely new both to WPF as to MVVM (I understand how it works, but have never used it...) Every single tutorial/article I find on the web, it uses either RelayCommand, or the DelegateCommand. In my opnion, these patterns obligues the VM to violate the SRP principle, since it will hold the command logic inside them. Why not just use a custom implementation of the ICommand interface? Just like this: Imagine that you're displaying a person and saving

How to Unit Test DelegateCommand that calls async methods in MVVM

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 13:24:10
问题 I am new to Unit Testing MVVM and using PRISM on my project. I am implementing Unit Testing on our current project and not having luck finding resources online that would tell me how totest DelegateCommand that calls async method. This is a follow up question to my post - How to Unit Test a ViewModel with async method. on how to unit test an async methods in MVVM and was answered that public methods can be tested using async TestMethod. This scenario will work only if the method that I want

How to Unit Test DelegateCommand that calls async methods in MVVM

≯℡__Kan透↙ 提交于 2019-12-28 13:22:03
问题 I am new to Unit Testing MVVM and using PRISM on my project. I am implementing Unit Testing on our current project and not having luck finding resources online that would tell me how totest DelegateCommand that calls async method. This is a follow up question to my post - How to Unit Test a ViewModel with async method. on how to unit test an async methods in MVVM and was answered that public methods can be tested using async TestMethod. This scenario will work only if the method that I want

How to make the CanExecute trigger properly using Prism 6

无人久伴 提交于 2019-12-23 01:43:38
问题 I have a Model public class Irritant : BindableBase { private short _id; private string _name; private string _description; public short Id { get { return _id; } set { SetProperty(ref _id, value); } } public string Name { get { return _name; } set { SetProperty(ref _name, value); } } public string Description { get { return _description; } set { SetProperty(ref _description, value); } } public Irritant() { Id = 0; Name = ""; Description = ""; } } Then my ViewModel with two versions public