I need to add an event handler to an inherited control like a datagridview but Visual Studio doesn't allow me. Isn't there a way for an inherited control to fire a base event handler AND the inherited one? in a sequence I specify?
Your question is unclear. Assuming that you're trying to handle the base class' event in the inherited control, you can override the OnEventName protected virtual method. In the override, make sure to call the base method or the event won't fire.
This method exists (AFAIK) for every event in every control in System.Windows.Forms. If the control you're inheriting does not have this virtual method, you can subscribe to the vent in the constructor.
For example:
class MyButton : Button {
//First way
protected override void OnClick(EventArgs e) {
base.OnClick(e); //Without this line, the event won't be fired
//...
}
//Second way
public MyButton() {
base.Click += Base_Click;
}
void Base_Click(object sender, EventArgs e) {
//...
}
}
EDIT:
If you're trying to raise the base class' event, you can call these OnEventName methods.
If the base class doesn't have one, the only way to do it is to declare a new event, and add a handler to the original event from the base class that raises your new event. Note that if you have other code that uses the base class' event, and the evnet is not virtual and doesn't have a raiser method, you're out of luck, unless you can decompile the base clas and find out where the event is raised.
Events in WinForms typically have a corresponding "On*" method. Simply call that method, and it'll raise the event. If you want to raise "CellClick" for example, call "base.OnCellClick(new DataGridViewCellEventArgs(row, column))".
Related
I will appreciate if some body can explain with a simple example.
Imagine a Window containing a dense hierarchy of child controls. Now let's say you want to do something, there's a right click anywhere in your window.
With normal events, you'd have to handle a Click event for all controls, because you're not sure where the user might click.
With WPF's routed events, the events either "bubble" or "tunnel" (i.e travel up the UI tree or down) if they dont find an event handler, which "handles" it at the current level. So you could write one handler for the window's event i.e. TopLevel. (WPF has a convention of event pairs, PreviewXXX and XXX - the PreviewXXX event fires first and tunnels down from root to control which received the stimulus and the counterpart XXX event then bubbles up from child control back upto Root). So if you right click a button, WPF travels up the UI hierarchy, invoking all handlers that it finds (unless someone marks the event has "handled" in the event args.)
Routed events are events with more 'traveling abilities', as mentioned in a Gishu's answer. Routed events are represented by an instance of a RoutedEvent class + ordinary .NET event, which wraps it:
public class MyClassWithARoutedEvent : UIElement
{
public static readonly RoutedEvent DoSomethingEvent;
public event RoutedEventHandler DoSomething
{
add { base.AddHandler ( MyClassWithARoutedEvent.DoSomethingEvent, value );
remove { base.AddHandler ( MyClassWithARoutedEvent.DoSomethingEvent, value );
}
}
You would typically use touted events in such situations:
Implementing your own control which seamlessly integrates with WPF's infrastructure
Processing events, fired by different controls at a common root
Sort of communication between elements in an element tree
In most situations you will probably use the routed events infrastructure without even noticing it.
In addition it's worth to mention, that you can use RoutedEvent in your control even if it does not define it or even inherits from an element, which does. That's because you can really think about a RoutedEvent instance as a strong typed name of an event. So, if you have an access to this 'name' (this is why an instance of a routed event is usually made public), you can owe it:
public class MyClassWithARoutedEvent : UIElement
{
public static readonly RoutedEvent ClickEvent;
static MyClassWithARoutedEvent ( )
{
ClickEvent = ButtonBase.ClickEvent.AddOwner( typeof ( MyClassWithARoutedEvent ) );
}
// A wrapper should be placed here as described above
}
I've got a usercontrol (MyUC) that is programatically added to a page (MainPage) several times.
In MyUC I set the DataContext to a view model like this:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
DataContext = new MyUCViewModel();
}
In my view model (MyUCViewModel) I have a collection of data items:
public MyDataItemCollection MyDataItems { get; private set; }
And in the constructor I have:
public MyUCViewModel()
{
this.MyDataItems = new MyDataItemCollection();
this.MyDataItems.ChosenItems.CollectionChanged += new NotifyCollectionChangedEventHandler(ChosenItemsChanged);
this.MyDataItems.Add(new DataItem());
}
From the above MyDataItems has another collection in it for ChosenItems and I added a NotifyCollectionChangedEventHandler to it.
Other parts of my code add and remove from the ChosenItems collection and that part seems to work ok.
Next I have the event handler method in the view model:
private void ChosenItemsChanged(object sender, EventArgs e)
{
MessageBox.Show("Chosen Items Changed");
}
This also works and I get a messagebox everytime the user makes a change in the UI that affects the ChosenItems collection.
The part I'm trying to figure out now is how do I set it up so that my MainPage does something when the ChosenItemsChanged event fires in my user controls. What I want to do is have the MainPage loop through the generated MyUC controls on the page and make each usercontrol call a method.
You can add more event listeners in the MainPage like this:
MyUCViewModel viewModel = myUC.DataContext;
viewModel.MyDataItems.ChosenItems.CollectionChanged
+= new NotifyCollectionChangedEventHandler(MainPage_ChosenItemsChanged);
This is based on the comment as the question was a little misleading:
While not strictly MVVM, as your question appears to be, your should write your User Controls as if it was a third-party control and simply expose a custom event on it. User Controls should always be a black-box with a public interface. For a reusable control that is self-contained (as many are) MVVM is overkill.
e.g.
in your User Control add:
public event EventHandler<MyEventArgs> MyEvent
Create a MyEventArgs class deriving from EventArgs and get it to hold useful parameters (like the selected item).
In your main page add a handler to MyEvent on each User Control you dynamically add.
I actually think the MVVM model is flawed and all this sort of controlling logic and event handlers belong in a Controller class (MVCVM!), but that's another story. :)
Please check the code below which defines the same scroll event in two different ways:
---------------------------------normal way--------------------------------------
public event RoutedEventHandler CloseTab;
------------------------------static RoutedEvent---------------------------------
public static readonly RoutedEvent CloseTabEvent =
EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CloseableTabItem));
public event RoutedEventHandler CloseTab
{
add { AddHandler(CloseTabEvent, value); }
remove { RemoveHandler(CloseTabEvent, value); }
}
1.What's the difference between the normal customized event and the static RoutedEvent?
2.Why the event in the first way can be raised by a method like "if(CloseTab!=null){CloseTab(o,e);}",but in the second way ,it only can be raised by the UIElement.RaiseEvent() method?
3.I know this question must be very very silly,but it dose make me confused.
It is that Why the event in the first way can be raised by the "if(CloseTab!=null){CloseTab(o,e);}" method only in the class which the event is defined?And when it is outside the owner class,the event can only add or remove handlers with "+=/-=" ?
Thanks for your time!!
You implementation basics i.e. the way we raise each one differently, are correct.
But apart from that, as far as my knowldge on routed events goes...
The first is NOT a routed event. It is a simple CLR event declared with its type as the RoutedEventHandler delegate. When this event is raised it will not bubble \ tunnel up to the ancestor/child UI elements respectively.
At practical level, I guess if you try using the first one in EventTrigger it will not work.
I have an Inkcanvas in my project (myPaint)
What is the name of event for add or remove the children (UiElement) from InkCanvas. for example I want handle this event : myInkCanvas.Children.remove(myRectangle) or this example :
myInkCanvas.Children.Add(myRectangle)
There isn't an event you can listen to that is fired when elements are added to or removed from the Children collection. There is a virtual protected method that is called, which you could leverage, called OnVisualChildrenChanged.
This isn't directly tied to the Children collection, as elements can add/remove visuals separate from that. But for InkCanvas, it would probably be safe.
So you'd use something like:
public class MyInkCanvas : InkCanvas {
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved) {
// TODO: Raise event or do something
base.OnVisualChildrenChanged(visualAdded, visualRemoved);
}
}
What exactly do you need this for? Can you use the StrokeCollected event instead?
I want to create a UserControl with several controls inside. These controls should behave simmilar to the radio buttons, i. e., the status of one of them affects the status of the rest. To do that I would like to be able to handle the OnClick event on each of the controls from the parent.
One solution could be to call a method of the parent, to perform the global UserControl change, from the child controls' OnClick method. Something like:
class Child : UserControl
{
...
protected override void OnClick(EventArgs z_args)
{
// do own stuff
((PartentType)Parent).ChangeStatus(this);
}
...
}
This is a solution, but I wonder if there is a more standard an elegant way to solve this issue. Thanks!
No, this is very bad, a control should never depend on having a specific parent. Do it the same way any Windows Forms control does it: if something interesting happens that a parent might be interested in then raise an event:
public event EventHandler StatusChanged;
public int Status {
get { ... }
}
protected override void OnClick(EventArgs z_args) {
// do own stuff
//...
var handler = StatusChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
Consider putting the event raising code in a private setter for the Status property.