locked
Invoking multiple command targets from a single command source RRS feed

  • שאלה

  • 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 as 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. 




    • נערך על-ידי eranotz65 יום ראשון 30 ספטמבר 2012 22:11
    יום ראשון 30 ספטמבר 2012 21:44

כל התגובות