2012-09-30 11 views
0

我需要調用命令從單個命令源

的多個實例在這個例子中我帶2個控制「A」和「B」

「A」。將調用多個命令目標調用者和 'B' 的invokie和有 'B' 的多個實例

的控制:

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));    
} 

綁定:

<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" /> 

我需要的是所有B個命令綁定到一個命令執行OnACommand時執行。

,我會覺得工作的唯一方法是,如果我實施命令B束縛OneWayTosource,但只比最後一個綁定到應是能得到執行的B中。

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" /> 

如果我必將像單向任何其他方式,我需要實現一個命令和B不知道 ,它甚至被執行,除非有可能一些如何從B內部的委託確認執行..

所以總結我需要從一個來源執行多個目標。

此外,我可能會指出,我解決了這個用我的「A1」 宣佈定期.NET事件和簽約的所有的B的,但因爲這是WPF寫在MVVM我要找的MVVM樣式使用命令執行此操作的方式。

在此先感謝。

回答

2

實現你正在努力完成的一種可能的方法是使用composite command

+0

非常感謝。 –