Why does nothing in WPF have a click event? - c#

This seems very strange to me.
I know through some adventurous inheritance you can convert most UIElements to a button, but it is a cumbersome way of implementing the most basic of of computer events

The ButtonBase class certainly has a Click event. So has derived classes.

I think the most basic events are MouseDown and MouseUp which are available to every UIElement

use RoutedEvents event if you really need to use.
Other wise use dependency property to register a Click event against your control and then do what ever you want to do.

You can add RoutedEvents to your controls.
Here is an implementation example

Related

When to use Event Properties in C# objects and when use just events

Would like to know the use-case wherein it is appropriate to use Event Properties instead of just Events and vice versa.
Well, obviously, event properties can be used to override the default behavior for adding/removing event handlers. For example if you want to make sure there's ever only one handler at a time, etc.
So my answer is: Unless there's something you don't like about the way event handlers are added/removed by default, keep your hands off event properties. If you have to do anything different from the defaults you'll have to implement event properties.
Personal opinion: In all the years I've been using C#/.NET now I have not once felt the need to add an event property...

Know which textBox has focus in xaml?

I have 3 textboxes that will change while it's being typed on a 4th one. I have tried the TextChanged="function" and change the content of the other three, but by chaging the .Text property, the event TextChanged is triggered again providing an undesired result. I was thinking of checking which textbox has the focus at the time, but I have no idea on how to implement that. I am experienced with Java and I'm very frustrated with all this c# and XAML. Thank you in advance.
If you search for FocusManager then you will find two kind of FocusManager class. But you have to go for
Windows.UI.Xaml.Input.FocusManager
not
System.Windows.Input.FocusManager.
It has static method GetFocusedElement() which queries the focus system to determine which object in the UI has focus.
There is a FocusManager-class, which might help you. Use the textbox' parent as scope-element.

WPF: Get MouseEnter Event When IsEnabled=false

I want to handle MouseEnter event for a Custom Control when control is disabled.
Is there a way to handle it?
The documentation for UIElement.IsEnabled documents the behaviour you see:
Elements that are not enabled do not participate in hit testing or focus and therefore will not be sources of input events.
The logical conclusion, to me, is that if you do want to handle mouse events, you don't disable the control. Instead, use some other method of achieving what you want. For example, if it's an input control, it may be good enough to make it read-only instead of disabled. Your question doesn't really explain why you want this, so I cannot guess what the right method for you will be.
perhaps you could surround your custom control with a ContentControl (which must always be enabled) and handle MouseEnter event on the ContentControl.

Get event handler of MenuItem programmatically

I defined this menu item in xaml:
<MenuItem
Header="header of item"
Click="eventhandler_of_item"
Name="nameofitem"/>
Now I would like get the event handler of the click event programmatically.
Getting the header is easy:
string header = nameofitem.Header.ToString();
But getting the click event handler seems to be a bit more tricky.
Could anyone give a hint?
It's messy, i would recommend you use commands, they can be passed around easily.
I dont think, that this is possible. You only can add or remove an event-handler (register and unregister).
As far as I know, there is no possibility to iterate through the attached handlers (I wrote iterate, because potentially there can be more than one handler attached to an event).
For CLR-events, I remember that I have seen once a solution based on reflection.
As H.B. stated (+1), use Commands, this are "normal" types and can be used as you desire...

WPF Mouse Over

What is the best way to link a method when the mouse over event is fired?
An example.
when a mouse hovers over a button. A method fires such as count.
So as long as the mouse is over the button, the count will keep on incrementing.
How will i connect a mouse over event to a method? Using Xaml?!
ANY IDEAS?!
One way to do this is via an attached property, especially if you want to reuse this capability.
Expression Blend's Behavior<T> class makes this simple. You'll need the the Blend SDK, but then you can use behaviors directly. Here's a blog post showing an example.
This allows you to assign an attached behavior in XAML. Many MVVM frameworks have an implementation that uses this approach, such as the LifetimeEvents in Cinch.

Categories