DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Effective Java Collection Framework: Best Practices and Tips
  • Microservices With Apache Camel and Quarkus
  • Writing a Vector Database in a Week in Rust
  • How To Approach Java, Databases, and SQL [Video]

Trending

  • Effective Java Collection Framework: Best Practices and Tips
  • Microservices With Apache Camel and Quarkus
  • Writing a Vector Database in a Week in Rust
  • How To Approach Java, Databases, and SQL [Video]
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. How to Get the EventArgs as a CommandParameter Using the AttachedCommandBehavior

How to Get the EventArgs as a CommandParameter Using the AttachedCommandBehavior

Marlon Grech user avatar by
Marlon Grech
·
Jul. 06, 09 · News
Like (0)
Save
Tweet
Share
8.79K Views

Join the DZone community and get the full member experience.

Join For Free

I have been asked many times how can I get the event args as a command parameter when using the AttachedCommandBehaviour.

Well the library does not support such a feature. I can do that yet I feel like the EventArgs should not flow down to the ViewModel because as such that is a UI thing… Yet in some case (such as for example if you want to set the Handled property to true of the Event args) this is needed.

I decided not to add this in the library yet I am gonna show how one can get this to work with some Attached Behavior.

The idea is to have a Attached Dependency property that handles the event and stores the last Event Args in another Attached Dependency Property. Here is the code to do so:

public class MouseEventArgsHandler
{
#region LastMouseEventArgs

/// <summary>
/// LastMouseEventArgs Attached Dependency Property
/// </summary>
public static readonly DependencyProperty LastMouseEventArgsProperty =
DependencyProperty.RegisterAttached("LastMouseEventArgs", typeof(MouseButtonEventArgs), typeof(MouseEventArgsHandler),
new FrameworkPropertyMetadata((MouseButtonEventArgs)null));

/// <summary>
/// Gets the LastMouseEventArgs property. This dependency property
/// indicates ....
/// </summary>
public static MouseButtonEventArgs GetLastMouseEventArgs(DependencyObject d)
{
return (MouseButtonEventArgs)d.GetValue(LastMouseEventArgsProperty);
}

/// <summary>
/// Sets the LastMouseEventArgs property. This dependency property
/// indicates ....
/// </summary>
public static void SetLastMouseEventArgs(DependencyObject d, MouseButtonEventArgs value)
{
d.SetValue(LastMouseEventArgsProperty, value);
}

#endregion

#region HandleMouseDoubleClick

/// <summary>
/// HandleMouseDoubleClick Attached Dependency Property
/// </summary>
public static readonly DependencyProperty HandleMouseDoubleClickProperty =
DependencyProperty.RegisterAttached("HandleMouseDoubleClick", typeof(bool), typeof(MouseEventArgsHandler),
new FrameworkPropertyMetadata((bool)false,
new PropertyChangedCallback(OnHandleMouseDoubleClickChanged)));

/// <summary>
/// Gets the HandleMouseDoubleClick property. This dependency property
/// indicates ....
/// </summary>
public static bool GetHandleMouseDoubleClick(DependencyObject d)
{
return (bool)d.GetValue(HandleMouseDoubleClickProperty);
}

/// <summary>
/// Sets the HandleMouseDoubleClick property. This dependency property
/// indicates ....
/// </summary>
public static void SetHandleMouseDoubleClick(DependencyObject d, bool value)
{
d.SetValue(HandleMouseDoubleClickProperty, value);
}

/// <summary>
/// Handles changes to the HandleMouseDoubleClick property.
/// </summary>
private static void OnHandleMouseDoubleClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Control control = d as Control;
if (control != null)
{
if ((bool)e.NewValue)
control.MouseDoubleClick += ControlMouseDoubleClick;
else
control.MouseDoubleClick -= ControlMouseDoubleClick;
}
}

static void ControlMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
SetLastMouseEventArgs((DependencyObject)sender, e);
}

#endregion
}

then you can use that value as the CommandParameter… Something like this:

<ListBox ItemsSource="{Binding Data}" 
local:MouseEventArgsHandler.HandleMouseDoubleClick="True"
acb:CommandBehavior.Event="MouseDoubleClick"
acb:CommandBehavior.Command="{Binding DoIt}"
acb:CommandBehavior.CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(local:MouseEventArgsHandler.LastMouseEventArgs)}" />

and here is what the command in the ViewModel looks like:

DoIt = new SimpleCommand
{
ExecuteDelegate = x =>
{
//set the event as handled
((MouseButtonEventArgs)x).Handled = true;
System.Diagnostics.Debug.WriteLine("Event handled");
}
};

The down side of this is that you have to do an attached property for every different event you want to handle, yet at the same time this should be a rare case and if it is not then you should really re think what you are doing with MVVM.

Hope it helps :)

I created a demo project for anyone that wants to have a look.

 

Event Property (programming) Dependency Library Command (computing) Flow (web browser)

Published at DZone with permission of Marlon Grech. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Effective Java Collection Framework: Best Practices and Tips
  • Microservices With Apache Camel and Quarkus
  • Writing a Vector Database in a Week in Rust
  • How To Approach Java, Databases, and SQL [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: