如何将两个附加行为附加到一个XAML元素上? [英] How can I attach two attached behaviors to one XAML element?

查看:109
本文介绍了如何将两个附加行为附加到一个XAML元素上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了已附加命令行为模式在这里,并且它运行良好以允许例如一个Border,以具有在ViewModel中触发的左击或右击事件:

I've implemented the attached command behavior pattern found here and it works well to allow e.g. a Border to have a left- or right-click event that fires in the ViewModel:

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

但是,如何将两个附加行为附加到一个元素上,例如我想做类似以下的事情,但是它当然给我一个错误:

However, how do I attach two attached behaviors to one element, e.g. I want to do something like the following but it of course gives me an error:

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

推荐答案

您发送的链接包含该答案.您可以在ACB v2中使用CommandBehaviorCollection.Behaviors功能.

The link you sent contains that very answer. You can use the CommandBehaviorCollection.Behaviors capabilities in ACB v2.

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

这篇关于如何将两个附加行为附加到一个XAML元素上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆