WPF PRISM 6 DelegateComand ObservesCanExecute - wpf

Thanks in advance!
How should I use ObservesCanExecute in the DelegateCommand of PRISM 6?
public partial class UserAccountsViewModel: INotifyPropertyChanged
{
public DelegateCommand InsertCommand { get; private set; }
public DelegateCommand UpdateCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public UserAccount SelectedUserAccount
{
get;
set
{
//notify property changed stuff
}
}
public UserAccountsViewModel()
{
InitCommands();
}
private void InitCommands()
{
InsertCommand = new DelegateCommand(Insert, CanInsert);
UpdateCommand = new DelegateCommand(Update,CanUpdate).ObservesCanExecute(); // ???
DeleteCommand = new DelegateCommand(Delete,CanDelete);
}
//----------------------------------------------------------
private void Update()
{
//...
}
private bool CanUpdate()
{
return SelectedUserAccount != null;
}
//.....
}
Unfortunatelly, I'm not familiar with expressions in c#. Also, I thought this would be helpful to others.

ObservesCanExecute() works “mostly like” the canExecuteMethod parameter of DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod).
However, if you have a boolean property instead of a method, you don't need to define a canExecuteMethod with ObservesCanExecute.
In your example, suppose that CanUpdate is not a method, just suppose that it's a boolean property.
Then you can change the code to ObservesCanExecute(() => CanUpdate) and the DelegateCommand will execute only if the CanUpdate boolean property evaluates to true (no need to define a method).
ObservesCanExecute is like a “shortcut” over a property instead of having to define a method and having passing it to the canExecuteMethod parameter of the DelegateCommand constructor.

Related

Tick the checkbox column in WPF datagrid through code

I have an ObservableCollection of items in which one of the property is bool.
When i set the itemsSource of the datagrid as the ObservableCollection, it auto-generates the column with checkbox column for the bool property.
I would like to know how we can tick the checkbox in code, lets say if we have the mark all option?
I tried updating the ObservableCollection records property value with true, but it doesnt help updating the UI.
Please help.
[EDIT: Below code works as suggested in the answer]
My Class is as follows
public class InvoiceDoc : INotifyPropertyChanged
{
private bool _Selected;
[DisplayName("Selected")]
public bool Selected
{
get { return _Selected; }
set { _Selected = value; this.OnPropertyChanged(); }
}
[DisplayName("Date")]
public DateTime DocDate { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged !=null)
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
The datagrid is as follows
<DataGrid x:Name="dgInvoices" Margin="32,110,32,59" AutoGeneratingColumn="dgInvoices_AutoGeneratingColumn"/>
setting the ItemsSource is as follows
docs = new ObservableCollection<InvoiceDoc>(); ;
dgInvoices.ItemsSource = docs;
I am expecting the grid to auto check the check box once is set the value in the collection.
Binding to an ObservableCollection is only reactive if an Item is added or removed.
Your elements inside your Collection have to implement INotifyPropertyChanged so the UI recognises the changes
EDIT:
Lets say you have the following objects in your Collection:
public class MyClass {
public string Name { get; set; }
public bool IsActive { get; set; }
}
This class has now to be modified to the following:
public class MyClass : INotifyPropertyChanged{
private string _name;
private bool _isActive;
public string Name
{
get { return this._name; }
set { this._name = value; this.OnPropertyChanged();}
}
public bool IsActive
{
get { return this._isActive; }
set { this._isActive = value;
this.OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null) {
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
If there are any Errors, remove the CallerMemberNameAttribute and invoke the this.OnPropertyChanged(); with the Propertyname.

Unit Testing ICommand with NSubstitute

I have the following ViewModel
public class MyViewModel : IMyViewModel
{
private readonly IMyModel myMode;
private ICommand _myCommand;
public MyViewModel(IMyModel model)
{
_model = model;
}
public ICommand MyCommand
{
get { return _myCommand ?? (_myCommand = new RelayCommand(x => MyMethod())); }
}
private void MyMethod()
{
_model.SomeModelMethod();
}
}
where IMyViewModel is defind as
public interface IMyViewModel
{
ICommand MyCommand { get; }
}
and my interface for the model is defined as
public interface IMyModel
{
void SomeOtherCommand();
}
Now in my unit test (using NSubstitute) I want to check that when MyCommand is invoked my model receives a call to its method SomeModelMethod. I've tried:
[TestMethod]
public void MyViewModel_OnMyCommand_CallsSomeOtherMethodOnModel()
{
var model = Substitute.For<IMyModel>();
var viewModel = Substitute.For<IMyViewModel>();
viewModel.MyCommand.Execute(null);
model.Received().SomeOtherMethod();
}
but this doesn't currently work. How do I best test that my Model method is called when a command on my ViewModel is invoked?
Not sure why you're mocking IMyViewModel here. You said you wanted to test whether SomeOtherMethod is invoked when you execute the command in MyViewModel.
You shouldn't be mocking the MyViewModel here.
[TestMethod]
public void MyViewModel_OnMyCommand_CallsSomeOtherMethodOnModel()
{
var model = Substitute.For<IMyModel>();
var viewModel = new MyViewModel(model);
viewModel.MyCommand.Execute(null);
model.Received().SomeOtherMethod();
}
P.S: I'm not familiar with nsubstitute. But the idea is still same (you shouldn't mock MyViewModel). Make sure you're using the right methods in nsubstitute.

I have a class object in one view model. I would like to pass this object state to another View Model in MVVM

In AttachmentViewModel I have the following code
public ICommand EditAttachmentCommand { get; private set; }
public AttachmentsViewModel()
{
EditAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
In my MainViewModel I have the following code:
public ICommand SessionAttachmentCommand { get; private set; }
public MainMenuViewModel()
{
SessionAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
I would like to pass the object state of AvailAttachment class from AttachmentsViewModel to MainMenuViewModel. presently null value comes for AvailAttachment's attachment object in MainMenuViewModel. I am debugging a code written by someone. This is a silverlight project using MVVM model. How do I do that?
Any help is appreciated. Thanks

How to pass CommandParameters to the ViewModel?

I have a command that should switch the current view when it's executed. I binded this command to my buttons like this:
<Button Style="{StaticResource TextButton}" Command="{Binding ViewModel:MainViewModel.OpenItemCommand}" CommandParameter="{Binding Link}"/>
I want to pass Link (the link of the currently selected article) to my command. My command is defined like this:
public class Command : ICommand
{
public event EventHandler CanExecuteChanged;
readonly Predicate<Object> _canExecute;
readonly Action<Object> _executeAction;
public Command(Predicate<Object> canExecute, Action<object> executeAction)
{
_canExecute = canExecute;
_executeAction = executeAction;
}
public bool CanExecute(object parameter)
{
if (_canExecute != null)
return _canExecute(parameter);
return true;
}
public void UpdateCanExecuteState()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, new EventArgs());
}
public void Execute(object parameter)
{
if (_executeAction != null)
_executeAction(parameter);
UpdateCanExecuteState();
}
}
In my ViewModel I have this:
public ICommand OpenItemCommand
{
get
{
if (_openItemCommand == null)
{
_openItemCommand = new Command.Command(
p => true,
p => OpenItem(_HOW_DO_I_GET_THE_PARAMETER?_)
);
}
return _openItemCommand;
}
set
{
if (_openItemCommand != value)
{
_openItemCommand = value;
RaisePropertyChanged("OpenItemCommand");
}
}
}
private void OpenItem(Uri link)
{
throw new NotImplementedException();
}
When I create the command I need to pass the command parameter (the link) to the Execute method. But how do I get the value of this? I defined the CommandParameter in XAML, but I don't know how to access it.
I really searched through a huge amount of websites but I can't really find the answer.
You should look at the implementation of Prism's DelegateCommand or MVVM light's RelayCommand. With these you would write code like this:
public class ViewModel
{
public ViewModel()
{
OpenItemCommand = new RelayCommand<string>(OpenItem);
}
public RelayCommand<string> OpenItemCommand { get; private set; }
private void OpenItem(string link)
{
Debug.WriteLine(link);
}
}
where string in this case is the type of the parameter.
I'm not sure where the link parameter is coming from but if it's from a control, the value of the control could be bound to a property of your view model, then you don't need a parameter, for example:
public class ViewModel
{
public ViewModel()
{
OpenItemCommand = new RelayCommand(OpenItem);
}
public RelayCommand OpenItemCommand { get; private set; }
public string Link { get; set; }
private void OpenItem()
{
Debug.WriteLine(Link);
}
}
replace
p => OpenItem(_HOW_DO_I_GET_THE_PARAMETER?_)
with
p => OpenItem(p)
that is what the p stands for: parameter

Implementing INotifyCollectionChanged interface

I need to implement a collection with special capabilities. In addition, I want to bind this collection to a ListView, Therefore I ended up with the next code (I omitted some methods to make it shorter here in the forum):
public class myCollection<T> : INotifyCollectionChanged
{
private Collection<T> collection = new Collection<T>();
public event NotifyCollectionChangedEventHandler CollectionChanged;
public void Add(T item)
{
collection.Insert(collection.Count, item);
OnCollectionChange(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
protected virtual void OnCollectionChange(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, e);
}
}
I wanted to test it with a simple data class:
public class Person
{
public string GivenName { get; set; }
public string SurName { get; set; }
}
So I created an instance of myCollection class as follows:
myCollection<Person> _PersonCollection = new myCollection<Person>();
public myCollection<Person> PersonCollection
{ get { return _PersonCollection; } }
The problem is that the ListView does not update when the collection updates although I implemented the INotifyCollectionChanged interface.
I know that my binding is fine (in XAML) because when I use the ObservableCollecion class instead of myCollecion class like this:
ObservableCollection<Person> _PersonCollection = new ObservableCollection<Person>();
public ObservableCollection<Person> PersonCollection
{ get { return _PersonCollection; } }
the ListView updates
What is the problem?
In order for your collection to be consumed, you should implement IEnumerable and IEnumerator too. Although, you're probably better off subclassing ObservableCollection<T>

Resources