Reputation: 19
I have the following multibinding in my xaml file:
<MyControl:CommandParameter>
<MultiBinding Converter="{commonConverters:MultiBindingConverter}">
<Binding Path="CurrentItem.Id" />
<Binding Path="SelectedItem.Count" />
</MultiBinding>
</Mycontrol:CommandParameter>
How can i define this multibinding in my viewmodel? Or, when this is not possible, how can I define in my viewmodel that the CanExecute of the command is triggered every time the Id or Count changes?
Another difficulty is that CurrentItem and SelectedItem can be null after initialization and will be initialized while using the application.
Thanks!
Upvotes: -1
Views: 87
Reputation: 2380
To tell WPF your command may have become (not) executable you can use the ICommand.RaiseCanExecuteChanged
event. It'll make WPF call the CanExecute
method of your command.
Since you didn't provide your ViewModel's and SelectedItem/CurrentItem's code the following example may not represent your use case but it'll give you the general idea.
Consider having the following custom command class :
public class MyCommand : ICommand
{
public EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
// do some stuff
}
public bool CanExecute(object parameter)
{
// determine if command can be executed
}
public void RaiseCanExecuteChanged()
{
this.CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
In your ViewModel you could then have something that looks like this
public class MyViewModel
{
private int _id;
public MyCommand SomeCommand { get; set; }
public int Id
{
get => _id;
set
{
// do other stuff (ie: property changed notification)
SomeCommand.RaiseCanExecuteChanged();
}
}
}
Upvotes: 0