C# how to execute the markup event first? - c#

I have this markupExtension Class
[MarkupExtensionReturnType(typeof(FrameworkElement))]
[ContentProperty("content")]
public class InsereSom : MarkupExtension
{
public InsereSom()
{ }
[ConstructorArgument("Ligado")]
public bool Ligado
{
get;
set;
}
[ConstructorArgument("Evento")]
public RoutedEvent Evento
{
get;
set;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
FrameworkElement elemento = target.TargetObject as FrameworkElement;
RoutedEventHandler metodo = new RoutedEventHandler(EventoInsereSom);
elemento.AddHandler(Evento, metodo);
EventInfo eventInfo = elemento.GetType().GetEvent("Click");
FrameworkElement parentClass = (MainWindow)((Grid)elemento.Parent).Parent;
Delegate methodDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, parentClass, "Button_Click");
eventInfo.RemoveEventHandler(elemento, methodDelegate);
eventInfo.AddEventHandler(elemento, methodDelegate);
return new System.Windows.Controls.Label();
}
public void EventoInsereSom(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello Extension Markup");
}
And this Xaml
<Button Width="80" Height="25" Click="Button_Click" Name="BtnTeste">
<Cei:InsereSom Ligado="True" Evento="Button.Click"/>
</Button>
And this code behind
public void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Event code behind");
}
I'd like that my method in my markup class execute first than the method in the code behind.
I try to add and remove the EventHandler but for that I need the event name ("Button_Click"). But cant use it hard code.
are there any other way to to id?
Thanks.

I'd like that my method in my markup class execute first than the method in the code behind.
It's not possible, the order in which event handlers are called can only be controlled by the class that raises the event (the button in that case). It's like a newspaper: when you subscribe to it, you can't say "I want to receive my paper before my neighbor"...
However there is a way to have the markup extension detect the click before the code-behind: you can make it handle the PreviewClick event (which is the tunnelling version of Click)

Related

Passing an object between two UserControls and a main Form

So I have one main Form that works as the navigation bar and two UserControls that display some controls.
In UserControlsA I have some fields that require to be filled. With that data I create an Object that contains some information. I require to pass that object to UserControlsB so I can display some data there.
My idea was to make three instances of the object, one in the UserControlsA to get the information required for the object, one in the main form to get a "copy" of the object from UserControlsA, and one in UserControlsB that can get the information from the main Form.
However, this seems redundant and doesn't even work. Here's some code:
Main Form:
public partial class main : Form
{
public Object object { get; set; }
public UCA uca;
public UCB ucb;
public Form1()
{
InitializeComponent();
uca = new UCA();
ucb = new UCB();
panel2.Controls.Add(uca);
panel2.Controls.Add(ucb);
ucb.Visible = false;
uca.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
ucb.Visible = false;
uca.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
ucb.Visible = true;
uca.Visible = false;
}
}
UserControlsA:
public partial class UCA : UserControl
{
public Object object { get; set; }
public UCA()
{
InitializeComponent();
}
private void bUsage_Click(object sender, EventArgs e)
{
//Data is provided
object = new Object(data);
//I use var parent to try and access the object from the main form.
var parent = Parent as Form1;
object = parent.object;
}
}
UsercontrolB:
public partial class UCB : UserControl
{
public Object object { get; set; }
public UCB()
{
InitializeComponent();
}
public void updateData()
{
//I try to assign the object from the main form to this form's object.
var parent = Parent as Form1;
object = parent.object;
}
}
Using var Parent doesn't work. What can I do?
A couple of examples using the INotifyPropertyChanged Interface and an implementation that makes use of standard public events.
Related Documentation:
Windows Forms Data Binding
Change Notification in Windows Forms Data Binding
Interfaces Related to Data Binding
Using INotifyPropertyChanged:
The UserControl exposes a public Property (here, named CustomDataObject, simple string Type in the first example, object in the second. It can another Type of course).
The Property is decorated with the Bindable attribute. The BindingDirection here is more a description of the intent, there's no Template attached to it.
Two other standard Attributes are added:
DefaultValue defines the default value of a Property (the value assigned to the Property when the Control is created). It's used by the Code Generator to determine whether the current value should be serialized: it's not serialized if it matches the value set by the Attribute.
It's also used by the PropertyGrid to show, in bold, a non-default value selection or assignment.
DesignerSerializationVisibility specifies the how the Property should be serialized at design-time. Here, is set to DesignerSerializationVisibility.Visible, to signify that the Property should be serialized.
The INotifyPropertyChanged Interface can be seen as a simplified way to add Property bindings to more than one property, using the same event handler, to notify a change in value.
The default implementation of the Interface simply requires that a a public Event of type PropertyChangedEventHandler is added to the class.
When a Property value is changed, the setter just invokes the Event. There are slightly different ways to perform this action; here I'm using a OnPropertyChanged() method that uses the CallerMemberName Attribute to acquire the name of the Property that calls it. It's fairly common in both WinForms and WPF.
UCA UserControl:
The UserControl (see the visual example), has two Buttons that change the bound CustomDataObject Property value. Their Click action is handled by ButtonsAction_Click.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
public partial class UCA : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string m_DataObject = string.Empty;
public UCA() => InitializeComponent();
[Bindable(true, BindingDirection.TwoWay), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string CustomDataObject {
get => m_DataObject;
set {
if (m_DataObject != value){
m_DataObject = value;
OnPropertyChanged();
}
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private void ButtonsAction_Click(object sender, EventArgs e)
{
var btn = sender as Button;
CustomDataObject = (btn == SomeButton) ? txtInput1.Text : txtInput2.Text;
}
}
UCB UserControl:
This other UserControl is the receiver. It just exposes a public Property (ReceiverDataObject) that will be bound to the CustomDataObject Property of UCA.
The ReceiverDataObject property is also defined as [Bindable], with the intention of making it one-way only. The property doesn't raise any event. It receive a value, stores it in a private Field and sets an internal UI element.
public partial class UCB : UserControl
{
private string m_RecvDataObject = string.Empty;
public UCB() => InitializeComponent();
[Bindable(true, BindingDirection.OneWay)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string ReceiverDataObject {
get => m_RecvDataObject;
set {
m_RecvDataObject = value;
txtPresenter.Text = m_RecvDataObject;
}
}
}
Using Standard Events notifications:
You can also generate Property change notifications using standard Events.
The difference is that you need an Event for each Property that should notify changes.
If you already have Event delegates used for this, then it's probably a good choice, since there's very few to add: just call the protected method that raises the Event in the Property setter.
Here, I'm, using the common .Net Event handling, using the EventHandlerList defined by the underlying Component class and exposed by its Events property, to add remove event subscriptions.
The Events are usually raised calling a protected method that has the same name of the Event, except the On prefix.
Here, CustomDataObjectChanged Event => OnCustomDataObjectChanged() method.
You can see this pattern in all standard Controls.
▶ The CustomDataObjectChanged name assigned to the Event is not a choice: this event must have the same name of the Property and the Changed suffix.
This is the pattern, it's enough to just follow it.
UCA UserControl:
public partial class UCA : UserControl
{
private static readonly object Event_CustomDataObjectChanged = new object();
private object m_DataObject = null;
public UCButtonActions() => InitializeComponent();
[Bindable(BindableSupport.Yes, BindingDirection.TwoWay), DefaultValue(null)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public object CustomDataObject {
get => m_DataObject;
set {
if (m_DataObject != value){
m_DataObject = value;
OnCustomDataObjectChanged(EventArgs.Empty);
}
}
}
public event EventHandler CustomDataObjectChanged {
add {
Events.AddHandler(Event_CustomDataObjectChanged, value);
}
remove {
Events.RemoveHandler(Event_CustomDataObjectChanged, value);
}
}
protected virtual void OnCustomDataObjectChanged(EventArgs e)
{
if (Events[Event_CustomDataObjectChanged] is EventHandler evth) evth(this, e);
}
}
UCB UserControl:
The second UserControl doesn't change. It's just the receiver.
The Form class (or another class used as Handler):
In the Form Constructor, or any other method called after the Form initialization, use the DataBindings property of UCB to link the Properties of the two UserControls:
public SomeForm()
{
InitializeComponent();
ucb1.DataBindings.Add("ReceiverDataObject", uca1, "CustomDataObject",
false, DataSourceUpdateMode.OnPropertyChanged);
}
You can also use a BindingSource to mediate:
BindingSource ucsSource = null;
public SomeForm()
{
InitializeComponent();
ucsSource = new BindingSource(uca1, null);
ucb1.DataBindings.Add("ReceiverDataObject", ucsSource, "CustomDataObject",
false, DataSourceUpdateMode.OnPropertyChanged);
}
Sample functionality:
Maybe you should redesign your data flow. UserControl should not usually make assumptions of what its parent would be, that's why it's a "customized control". It can be a Form1 but not necessary. So you shouldn't do casting like in your example.
To provide the information from A to B, one way is to create public Get/Set methods or properties for those controls. And the main form works with those public members, pseudo-code can be:
class main{
UCA uca;
UCB ucb;
public void RefreshData(){
object data = uca.GetData();
ucb.UpdateData(data);
}
}
So I just learned how to properly use events I guess. Here's how the code looks now:
Main form:
public partial class main : Form
{
public UCA uca;
public UCB ucb;
public delegate void passObject(object source, someObject u);
public Form1()
{
InitializeComponent();
uca = new UCA();
ucb = new UCB();
panel2.Controls.Add(uca);
panel2.Controls.Add(ucb);
ucb.Visible = false;
uca.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
uca.objectRequired += ucb.ucb_objectRequired;
}
private void button1_Click(object sender, EventArgs e)
{
ucb.Visible = false;
uca.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
ucb.Visible = true;
uca.Visible = false;
}
}
Usercontrol A:
public partial class UCA : UserControl
{
public someObject o { get; set; }
public event passObject objectRequired;
public UCA()
{
InitializeComponent();
}
private void bUsage_Click(object sender, EventArgs e)
{
//Data is provided
o = new someObject(data);
usageRequired?.Invoke(this, o);
}
}
Usercontrol B:
public partial class UCB : UserControl
{
public SomeObject o { get; set; }
public UCDetails()
{
InitializeComponent();
}
public void ucn_objectRequired(object sender, sObject u)
{
o = u;
//Use the data from the object.
}
}

How to call Prism Event in Code Behind

I'm new to WPF. Currently, I want to allow my Add button to add item by using either single click or double click. However, when I try to double click, it ends up fire single click event twice. Code in XAML as below:
<Button.InputBindings>
<MouseBinding Command="{Binding Path=AddCommand}" CommandParameter="{Binding}" MouseAction="LeftClick" />
<MouseBinding Command="{Binding Path=AddCommand}" CommandParameter="{Binding}" MouseAction="LeftDoubleClick" />
I found solution online which is to use DispatcherTimer in order to solve the problem. I have inserted these in code behind:
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer(
new TimeSpan(0, 0, 0, 1),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private void btnAdd_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
// Handle Double Click Actions
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
}
So here comes my question. I've removed the MouseBinding in XAML and want to call for AddCommand in code behind but I'm having problem to do so due to the PrismEventAggregator. The AddCommand in .cs as below:
private void AddCommandExecute(Object commandArg)
{
// Broadcast Prism event for adding item
this.PrismEventAggregator.GetEvent<AddItemEvent>().Publish(
new AddItemPayload()
{
BlockType = this.BlockType
}
);
}
Hence would like to know how to call for the AddCommand (which is a Prism Event in .cs) in Code behind?
Note: The button is inside resource dictionary thus I failed to use the button name to call for the command.
You need to create a class which will subscribe to the event you are publishing and then execute the logic you want.
For example:
public class AddItemViewModel : INotifyPropertyChanged
{
private IEventAggregator _eventAggregator;
public AddItemViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<AddItemEvent>().Subscribe(AddItem);
}
private void AddItem(AddItemPayload payload)
{
// Your logic here
}
}
Then when you publish the event it will trigger the subscriber and execute.
Using Expression Blend SDK, you can create a Behavior that encapsulates all your custom logic. This behavior will offer two dependency properties for your command and its parameter, so you can easily create Bindings for them, exactly as you do this for your InputBindings.
Move your event handlers and DispatcherTimer logic into this behavior:
using System.Windows.Interactivity;
class ClickBehavior : Behavior<Button>
{
// a dependency property for the command
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand),
typeof(ClickBehavior), new PropertyMetadata(null));
// a dependency property for the command's parameter
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object),
typeof(ClickBehavior), new PropertyMetadata(null));
public ICommand Command
{
get { return (ICommand)this.GetValue(CommandProperty); }
set { this.SetValue(CommandProperty, value); }
}
public object CommandParameter
{
get { return this.GetValue(CommandParameterProperty); }
set { this.SetValue(CommandParameterProperty, value); }
}
// on attaching to a button, subscribe to its Click and MouseDoubleClick events
protected override void OnAttached()
{
this.AssociatedObject.Click += this.AssociatedObject_Click;
this.AssociatedObject.MouseDoubleClick += this.AssociatedObject_MouseDoubleClick;
}
// on detaching, unsubscribe to prevent memory leaks
protected override void OnDetaching()
{
this.AssociatedObject.Click -= this.AssociatedObject_Click;
this.AssociatedObject.MouseDoubleClick -= this.AssociatedObject_MouseDoubleClick;
}
// move your event handlers here
private void AssociatedObject_Click(object sender, RoutedEventArgs e)
{ //... }
private void AssociatedObject_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{ //... }
// call this method in your event handlers to execute the command
private void ExecuteCommand()
{
if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
{
this.Command.Execute(this.CommandParameter);
}
}
The usage is very simple. You need to declare your additional namespaces:
<Window
xmlns:local="Your.Behavior.Namespace"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
...
Finally, attach the behavior to the button:
<Button>
<i:Interaction.Behaviors>
<local:ClickBehavior Command="{Binding AddCommand}" CommandParameter="{Binding}"/>
</i:Interaction.Behaviors>
</Button>

Disabled button in xaml

I am having a problem with xaml ... a button I have created is not enable. here is the xaml part:
<Button Margin="0,2,2,2" Width="70" Content="Line"
Command="{x:Static local:DrawingCanvas.DrawShape}"
CommandTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}}, Path=DrawingTarget}"
CommandParameter="Line">
</Button>
Before Constructor it goes:
public static RoutedCommand DrawShape = new RoutedCommand();
in ctor I have:
this.CommandBindings.Add(new CommandBinding(DrawingCanvas.DrawShape, DrawShape_Executed, DrawShapeCanExecute));
Then I have:
private void DrawShapeCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true; **//Isn't this enough to make it enable?**
en.Handled = true;
}
private void DrawShape_Executed(object sender, ExecutedRoutedEventArgs e)
{
switch (e.Parameter.ToString())
{
case "Line":
//some code here (incomplete yet)
break;
}
When I remove the first line (Command="{x:Static ...}") in the block it gets enable again!
Be sure the CanExecute property of that command is returning true. If it returns false, it automatically disables the control that utilizes that command.
Can execute should return a bool, I'm a little surprised that doesn't give a compile error. Anyway try to change it to this.
private bool DrawShapeCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
return true;
}
EDIT:
Ok since you just revealed all you want is a simple button that executes a command here's a very simple implementation copied from one of my recent projects. First define this class somewhere.
public class GenericCommand : ICommand
{
public event EventHandler CanExecuteChanged { add{} remove{} }
public Predicate<object> CanExecuteFunc{ get; set; }
public Action<object> ExecuteFunc{ get; set; }
public bool CanExecute(object parameter)
{
return CanExecuteFunc(parameter);
}
public void Execute(object parameter)
{
ExecuteFunc(parameter);
}
}
Next define a command in your view model and define both the properties I created in the generic command (it's just the basic stuff that comes along with implementing the ICommand interface).
public GenericCommand MyCommand { get; set; }
MyCommand = new GenericCommand();
MyCommand.CanExecuteFunc = obj => true;
MyCommand.ExecuteFunc = obj => MyMethod;
private void MyMethod(object parameter)
{
//define your command here
}
Then just wire up the button to your command.
<Button Command="{Binding MyCommand}" />
If this is all too much for you (MVVM does require a little extra initial setup). You can always just do this...
<Button Click="MyMethod"/>
private void MyMethod(object sender, RoutedEventArgs e)
{
//define your method
}

Button EventHandler in form

The button which we can create on the form is written in terms of event handler in Form1.Designer.cs as
this.button1.Click += new System.EventHandler(this.button1_Click);
Here Click is public event EventHandler 's type and this EventHandler is a delegate as
public delegate void EventHandler(object sender, EventArgs e);
Now,
why can't it be '='(equals)
this.button1.Click = new System.EventHandler(this.button1_Click);
and also when I am passing the argument this.button1_Click, how does it match up to
void EventHandler(object sender, EventArgs e); delegate ? As here I have two arguments.
Please clear me with this.
Thank you
ttSo, let's see what event is.
Code, you are write
public event EventHandler MyEvent;
will compile to
private EventHandler MyEvent = null;
[MethodImp(MethodImplOptions.Synchronized)]
public void add_MyEvent(EventHandler value) {
MyEvent = (EventHandler)Delegate.Combine(MyEvent, value);
}
[MethodImp(MethodImplOptions.Synchronized)]
public void remove_MyEvent(EventHandler<NewMailEventArgs> value) {
MyEvent = (EventHandler)Delegate.Remove(MyEvent, value);
}
So, as you see, you cannot directly access to delegate and can only call += and -=, which is overridden for event class.
Also you can manually manage this mechanism by overriding methods += and -=.
You can do it like this:
public event EventHandler MyEvent
{
add { //your code for += here }
remove { //your code for -= here }
}
More about event and delegates you can read in book "CLR via C#". I found all of this in this book.
esentially, you are adding a handler to the event, not setting the one handler. you might want to have more handlers for an event. one handler should not preclude having other handlers because there might be multiple actions that you could want to take place in response to a single event that might happen in different classes and in different places and on different threads and under different conditions. += says make me a subscriber to this event (and potentially one subscriber among many).
What if you want to have multiple methods called on Click event. What you are doing with
this.button1.Click += new System.EventHandler(this.button1_Click);
is registering for this.button1_Click method to be invoked when Click event is raised. += adds handler and NOT assigns handler.
1/ it can not be '='(equals) because delegate is like a function pointer
2/ If you want to pass parameter to event button click, you have to make your own button class and implement Click event and have you own EventArgs
sample code:
public class MyEventArg
{
int _param1;
string _param2;
//you can add more param
public MyEventArg(int _param1,string _param2)
{
this._param1 = _param1;
this._param2 = _param2;
}
}
public delegate void MyButtonClickHandler(object sender, MyEventArg e)
public class MyButton:Control
{
public event MyButtonClickHandler OnMyClick;
//You can raise your event here
protected override void OnClick(EventArgs e)
{
MyEventArg e = new MyEventArg(1,"a");//just sample data here
this.OnMyClick(this,e);
}
}
In the form that contains MyButton class instant
public partial class Form1 : Form
{
MyButton myButton = new MyButton();
public Form1()
{
InitializeComponent();
myButton.OnMyClick += new MyButtonClickHandler(this.myButton_OnMyClicked);
}
private void myButton_OnMyClicked(object sender, MyEventArg e){
//your implementation
}
}
Dear Nagaraj Tantri,
For question 1: As said above, Due to Delegate can set up multi-event.
For question 2:As culithay said, if you want to pass custom arguments throug event buttion
click, if you want to use EventHandler and pass cutom own argument
you have to custom your control class and custom own event argument,
the custom event parameter CustomEventArg should inherit EventArg class.
You can take the sample code as below.
// Customs ColorChanged's event parameter.
public class ColorChangedEventArgs : EventArgs
{
private Color color;
public ColorChangedEventArgs(Color c)
{
color = c;
}
public Color GetColor
{
get { return color; }
}
}
//Add this method in your custom control
protected void ibtnTest_Click(object sender, ColorChangedEventArgs e)
{
//TODO;
}
You can also referen MSDN here

Binding Commands to Events?

What's a good method to bind Commands to Events? In my WPF app, there are events that I'd like to capture and process by my ViewModel but I'm not sure how. Things like losing focus, mouseover, mousemove, etc. Since I'm trying to adhere to the MVVM pattern, I'm wondering if there's a pure XAML solution.
Thanks!
Use System.Windows.Interactivity
…xmlns:i=http://schemas.microsoft.com/expression/2010/interactivity…
<Slider
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction
Command="{Binding MyCommand}"
CommandParameter="{Binding Text, ElementName=textBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider>
Make sure your project references the assembly System.Windows.Interactivity.
Source: MSDN Blog Executing a command from an event of your choice
[Update]
Have a look to to Microsoft.Xaml.Behaviors.Wpf (available since 03.12.2018) Official package by Microsoft.
Have a look at Marlon Grech's Attached Command Behaviour, it could be exactly what you're looking for
In order to handle events, you must have some code that attaches itself to the event and executes your command in response. The final goal is to have in XAML:
MouseMoveCommand="{Binding MyCommand}"
In order to achieve this you need to define an attached property for each event that you want to handle. See this for an example and a framework for doing this.
I implemented it using Attached Properties and Reflection. I cannot say it is the best implementation, but I will maybe improve it and it may be a good start for you.
public class EventBinding : DependencyObject
{
public static string GetEventName(DependencyObject obj)
{
return (string)obj.GetValue(EventNameProperty);
}
public static void SetEventName(DependencyObject obj, string value)
{
obj.SetValue(EventNameProperty, value);
var eventInfo = obj.GetType().GetEvent(value);
var eventHandlerType = eventInfo.EventHandlerType;
var eventHandlerMethod = typeof(EventBinding).
GetMethod("EventHandlerMethod", BindingFlags.Static | BindingFlags.NonPublic);
var eventHandlerParameters = eventHandlerType.GetMethod("Invoke").GetParameters();
var eventArgsParameterType = eventHandlerParameters.
Where(p => typeof(EventArgs).IsAssignableFrom(p.ParameterType)).
Single().ParameterType;
eventHandlerMethod = eventHandlerMethod.MakeGenericMethod(eventArgsParameterType);
eventInfo.AddEventHandler(obj, Delegate.CreateDelegate(eventHandlerType, eventHandlerMethod));
}
private static void EventHandlerMethod<TEventArgs>(object sender, TEventArgs e)
where TEventArgs : EventArgs
{
var command = GetCommand(sender as DependencyObject);
command.Execute(new EventInfo<TEventArgs>(sender, e));
}
public static readonly DependencyProperty EventNameProperty =
DependencyProperty.RegisterAttached("EventName", typeof(string), typeof(EventHandler));
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(EventBinding));
}
public class EventInfo<TEventArgs>
{
public object Sender { get; set; }
public TEventArgs EventArgs { get; set; }
public EventInfo(object sender, TEventArgs e)
{
Sender = sender;
EventArgs = e;
}
}
public class EventInfo : EventInfo<EventArgs>
{
public EventInfo(object sender, EventArgs e)
: base(sender, e) { }
}
public class EventBindingCommand<TEventArgs> : RelayCommand<EventInfo<TEventArgs>>
where TEventArgs : EventArgs
{
public EventBindingCommand(EventHandler<TEventArgs> handler)
: base(info => handler(info.Sender, info.EventArgs)) { }
}
Examples of usage:
View
<DataGrid local:EventBinding.EventName="CellEditEnding"
local:EventBinding.Command="{Binding CellEditEndingCommand}" />
Model
private EventBindingCommand<DataGridCellEditEndingEventArgs> _cellEditEndingCommand;
public EventBindingCommand<DataGridCellEditEndingEventArgs> CellEditEndingCommand
{
get
{
return _cellEditEndingCommand ?? (
_cellEditEndingCommand = new EventBindingCommand<DataGridCellEditEndingEventArgs>(CellEditEndingHandler));
}
}
public void CellEditEndingHandler(object sender, DataGridCellEditEndingEventArgs e)
{
MessageBox.Show("Test");
}
I don't think you can use it in pure XAML, but take a look at the Delegate Command.
Execute Command, Navigate Frame, and Delegating Command behaviour is a pretty good pattern. It is also can be used in the Expression Blend.
On the "best practices" side, you should think twice before converting an event to a command. Normally, command is something user does intentionaly, an event most often is just an interaction trail, and should not leave the view boundaries.

Categories