2009-05-29 80 views
0

我已經實施了attached command behavior pattern found here並且它很好地運作以允許例如邊境到有火災視圖模型一個左或右鍵單擊事件:如何將兩個附加行爲附加到一個XAML元素?

XAML:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
     c:CommandBehavior.Event="MouseLeftButtonDown" 
     c:CommandBehavior.Command="{Binding PressedLeftButton}" 
     c:CommandBehavior.CommandParameter="MainBorder123"> 
    <TextBlock Text="this is the click area"/> 
</Border> 

代碼背後:

public ICommand PressedLeftButton { get; private set; } 

public MainViewModel() 
{ 

    Output = "original value"; 

    PressedLeftButton = new SimpleCommand 
    { 
     ExecuteDelegate = parameterValue => { 
      Output = String.Format("left mouse button was pressed at {0} and sent the parameter value \"{1}\"", DateTime.Now.ToString(), parameterValue.ToString()); 
     } 
    }; 
} 

然而,怎麼辦我將兩個附加行爲附加到一個元素,例如我想要做類似下面的,但它當然給我一個錯誤:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
     c:CommandBehavior.Event="MouseLeftButtonDown" 
     c:CommandBehavior.Command="{Binding PressedLeftButton}" 
     c:CommandBehavior.CommandParameter="MainBorder123" 
     c:CommandBehavior.Event="MouseRightButtonDown" 
     c:CommandBehavior.Command="{Binding PressedRighttButton}" 
     c:CommandBehavior.CommandParameter="MainBorder123" 
     > 

回答

5

您發送的鏈接包含了非常答案。您可以在ACB v2中使用CommandBehaviorCollection.Behaviors功能。

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test"> 
     <local:CommandBehaviorCollection.Behaviors> 
       <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/> 
       <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/> 
     </local:CommandBehaviorCollection.Behaviors> 
     <TextBlock Text="MouseDown on this border to execute the command"/> 
    </Border> 
+0

就是這樣,謝謝,不過搞笑的是我的XAML編輯給我的錯誤「的附着性能‘行爲’型‘CommandBehaviorCollection’沒有被發現。」雖然我可以運行並編譯它,但爲什麼呢? – 2009-05-29 15:21:48

0

「就是這樣,謝謝,滑稽雖然我的XAML編輯給我的錯誤‘的附着性能‘行爲’型‘CommandBehaviorCollection’。’未找到,雖然我可以運行並編譯罰款,這是爲什麼?」

原因是允許命令行爲集合(這是一個附加屬性集合)的代碼實際上是一種XAML漏洞。你可以閱讀更多有關在這裏: http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!468.entry?sa=276442122