How can I attach two attached behaviors to one XAML element?

浪尽此生 提交于 2019-12-09 02:04:08

问题


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>

Code Behind:

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

回答1:


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>



回答2:


"that was it, thanks, funny though that my XAML editor gives me the error "The attachable property 'Behaviors' was not found in type 'CommandBehaviorCollection'." although I can run and compile it fine, why is that?"

The reason is that the code that allows the command behavior collection (which is an attached property collection) is actualy sort of a XAML loophole. You can read more about that here: http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!468.entry?sa=276442122



来源:https://stackoverflow.com/questions/926451/how-can-i-attach-two-attached-behaviors-to-one-xaml-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!