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

Tagged:

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

Edward TanguayH
2009-05-29 15:00:00 Scores:0

2 answers

Answer 1
Scores:2

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>
siz H
2009-05-29 15:09:00
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?
Edward Tanguay H
2009-05-29 15:21:48 Scores:0
I don't know, but I have never trusted the XAML editors. :)
siz H
2009-05-29 15:25:58 Scores:3
Answer 2
Scores:0

"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

ProvianceH
2009-07-29 16:16:08
Share |
View original post at stackoverflow.com

Related topics

How to attach the alpha value of an element to a slider?

Is it possible to bind the alpha value of an element to a slider? For example, this is code that allows the slider to change the height and top position of the element, but what is the syntax to control the alpha value in the Background attribute of the border? <Window x:Class="WpfApplication25.Window1" ...

Window.FindName can't find Border element that I've named through an attached property

I've got a xaml of a window with an unnamed border control somewhere inside. The border control uses an attached property that I wrote and the attached property basically sets the border control's Name property to a known value, like "placeholder". After the window is loaded, it tries to find ...

binding element focus with an attached property?

I'm trying to bind focus for my control to a property on the view model, like this: public class Focus { public static readonly DependencyProperty HasFocusProperty = DependencyProperty.RegisterAttached("HasFocus", ...

Attached behavior binding to element in controltemplate

I am adding a attached behaviour to a slider which will cause it to scroll some content when the thumb is dragged and held over a specific region. (Can't use a straightforward IsMouseOver trigger as the Slider Thumb has MouseCapture.) The behaviour has 3 properties: #region IsScrollHoverProperty ...

Inheritance of Attached Behaviors

I'd like to implement a set of similar attached behaviors for use in a WPF application. Since their all share a chunk of boilerplate code, that I don't really want to repeat for every one, I'd like to create a base behavior inherit from it. But since everything inside attached behaviors ...

Attached behaviors and styles

I use an attached behavior that allows a DoubleClick event to be wired to a command in a view model, as in the binding below: <ListBox Style="{StaticResource MasterListBoxStyle}" b:SelectionBehavior.DoubleClickCommand="{Binding EditCommand}" > I need multiple list boxes for a presentation, all of which will need ...

Does the .NET framework have any attached properties or attached behaviors built into it?

I'm basically curious if any Microsoft code (.NET Framework) has any files that show attached properties and attached behaviors. I'm aware of numerous examples out there on the internet, but I'm more curious about seeing how Microsoft codes it. ...

How to attach a property to an object using XAML

I'm trying to format my ListBoxItem template to include a image. I can add an image to the ListBoxItem but i'm not too sure on how i would go about setting the value for that image. Template for ListBoxItem: <Style TargetType="{x:Type ListBoxItem}"> ...

Animating WPF element in XAML using attached property?

I got my animation to work triggered by a property in my ViewModel. If I set my TargetProperty to "Width", the below code actually works in growing the image. Next, i wanted to actually move the image up and down. To do this, i added <Canvas> component around my image, ...

Get instance of another element via Xaml Binding (Attached or DependencyProperty)

I have a use case where I want to get at a reference to an element in Xaml from another element. For instance, consider this simplistic case. I have a UserControl called A and a UserControl called B, and somehow I want to register an attached property where B can ...