Created
August 3, 2017 15:44
-
-
Save Keboo/6d6cee4890f02bfb68888ee5805298b9 to your computer and use it in GitHub Desktop.
Composite Commands in WPF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CompositeCommand : ICommand | |
{ | |
public event EventHandler CanExecuteChanged; | |
private readonly IList<ICommand> _Commands; | |
public CompositeCommand(IList<ICommand> commands) | |
{ | |
if (commands == null) throw new ArgumentNullException(nameof(commands)); | |
_Commands = new List<ICommand>(commands); | |
foreach (ICommand command in _Commands) | |
{ | |
command.CanExecuteChanged += CommandsOnCanExecuteChanged; | |
} | |
} | |
private void CommandsOnCanExecuteChanged(object sender, EventArgs eventArgs) | |
{ | |
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | |
} | |
public bool CanExecute(object parameter) | |
{ | |
return _Commands.Aggregate(true, (current, command) => current & command.CanExecute(parameter)); | |
} | |
public void Execute(object parameter) | |
{ | |
foreach (ICommand command in _Commands) | |
{ | |
command.Execute(parameter); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CompositeCommandConverter : MarkupMultiValueConverter | |
{ | |
public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) | |
{ | |
var commands = values?.OfType<ICommand>().ToList(); | |
if (commands?.Any() == true) | |
{ | |
return new CompositeCommand(commands); | |
} | |
return Binding.DoNothing; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Button Content="Do Composite Command"> | |
<Button.Command> | |
<MultiBinding Converter="{valueConverters:CompositeCommandConverter}"> | |
<Binding Source="{x:Static md:DialogHost.CloseDialogCommand}" /> | |
<Binding Path="MyBoundCommand" /> | |
</MultiBinding> | |
</Button.Command> | |
</Button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class MarkupMultiValueConverter : MarkupExtension, IMultiValueConverter | |
{ | |
public override object ProvideValue(IServiceProvider serviceProvider) | |
{ | |
return this; | |
} | |
public abstract object Convert(object[] values, Type targetType, object parameter, CultureInfo culture); | |
public virtual object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) | |
{ | |
throw new InvalidOperationException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment