Reputation: 12533
i need to invoke multiple instances of a command
for this example i'll take 2 controls 'A' and 'B'
'A' would be the Invoker and 'B' the invokie and there are multiple instances of 'B'
the Controls :
public class A : Control
{
public A()
{}
public ICommand OnACommand
{
get { return (ICommand)GetValue(OnAProperty); }
set { SetValue(OnACommandProperty, value); }
}
public static readonly DependencyProperty OnACommandProperty =
DependencyProperty.Register("OnACommand", typeof(ICommand), typeof(A), new UIPropertyMetadata(null));
public bool Something
{
get { return (bool)GetValue(SomethingProperty); }
set { SetValue(SomethingProperty, value); }
}
public static readonly DependencyProperty SomethingProperty=
DependencyProperty.Register("Something", typeof(bool), typeof(A), new UIPropertyMetadata(false,OnSometingPropertyChanged));
private static void OnSometingPropertyChanged(...)
{
...
OnACommand.Execute(this.Value);
}
}
public class B : Control
{
public B(){ }
public ICommand OnBCommand
{
get { return (ICommand)GetValue(OnBCommandProperty); }
set { SetValue(OnBCommandProperty, value); }
}
public static readonly DependencyProperty OnBCommandProperty =
DependencyProperty.Register("OnBCommand", typeof(ICommand), typeof(B), new UIPropertyMetadata(null));
}
the Binding :
<local:B x:Name="B1" OnBCommand="{Binding ElementName=A1 , Path=OnACommand />
<local:B x:Name="B2" OnBCommand="{Binding ElementName=A1 , Path=OnACommand />
<local:A x:Name="A1" />
what i need is for all the B Commands Bound to that A command to Execute when Executing OnACommand.
the only approach which i would think to work is if i implemented the Command inside B and bound it OneWayTosource , but than only the last one the Bind to A would be the B which would get Executed .
public B()
{
OnBCommand = new RelayCommand<int>
(
value => { this.Value = value ....}
);
}
<local:B x:Name="B1"
OnBCommand="{Binding ElementName=A1,Path=OnACommand,Mode=OneWayToSource />
<local:B x:Name="B2"
OnBCommand="{Binding ElementName=A1,Path=OnACommand,Mode=OneWayToSource />
<local:A x:Name="A1" />
if i bound it any other way like OneWay i need to implement the Command in A and B has no idea that it was even executed, unless it's possible to some how acknowledge the execution from a delegate within B ..
so to summarize i need to execute multiple targets from one source.
in addition i might point out that i solved this using a regular .net event which i declared in 'A1' and Subscribed all the B's to,but since this is WPF written in MVVM i'm looking for the MVVM Style Way of doing this , using Commands .
thanks in advance.
Upvotes: 0
Views: 1791
Reputation: 3535
A possible approach to achieve what you are trying to get done is using a composite command.
Upvotes: 2