WPF How to bind the same event handler to two different controls - c#

I have a UI with a search text box and a button that that should be clicked when the user want to preform the search. (like a search engine UI)
I want that the same event handler will be called when the user hit the search button and when the user hit enter in the text box.
I can easily hack it but my guess is that WPF has it's own 'right' way of doing it.
So what is the WPF way of doing it right?
Thanks.

There are different delegates for click and keypressed events.
So extract your code in method
named like 'DoSearch', then connect different (mb anonymous) handlers to events and call DoSearch inside handlers

Related

CoreWindow.KeyDown doesn't get called if Enter is pressed with focus on clickable element

Is there any way I can still catch the keypress if Enter is pressed on a clickable element.
Because the Windows Store Apps API treats Enter as click event rather than a keypress if the focus is on a clickable element (i.e. a GridViewItem).
Unfortunately I have to let the user right-click on a GridViewItem which sets the focus to the element.
Moving the focus would be an option but I'd prefer not to do that if I don't have to.
It seems, this is not in the nature of WInrt. You can follow the specification provided on MSDN:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.keydown.aspx
You can still try the Key Down event of GridView
In WinRT, some events are user-initiated only. As a result, some WinRT APIs can only be invoked in those types of events. Button.Click is one of those events. As a result, you cannot programmatically raise this event because it would undermine the security feature that limits certain API to those types of events.
Make sense?
Best of luck!

Any change on page prompts a message before navigation

I want to prompt a message to the user Do You want to save the changes?? if he made some changes in any control on the current page before navigation to other page. what is the best way to do this. every control has their events. Like text-box key change event. combo box selectedItemChange event. But a lot of code have to be written in this scenario. I want this modification on each page of the project....
Thanks In Advance...
you'll atleast need to define the event handlers for the controls that can be changed.
Inside the event handlers you can just set a boolean variable to true, and check the value of the variable in OnNavigatedFrom method of the Page, and accordingly show the message.
If you use the Silverlight Navigation framework:
http://msdn.microsoft.com/en-us/library/cc838245(v=vs.95).aspx
There are events that get raised (navigating, navigated, etc...) where you can prompt the user and cancel the navigation.

Handling Event without use of Button?

I just created an Button in xaml so, it created an event for handling the button click.
I want to call the contents inside the event handler as a function. So, that i can call in some other place to satisfy my needs.
Currently i am working in Live tiles. examples that i got are all in the form of button click event to trigger the flip and cycle tiles.
I want to call those into function to make it work.
Or else any one give the example of Lives tiles from the beginning to end

Redirecting keyboard activity to a specific control

I have a C# WinForm application. On the main form, I have a user control. What I want to be able to do is, whenever a key is pressed on the keyboard, I would like to have my user control receive the keyboard input, so that the keyboard related events (KeyDown, KeyUp and KeyPress) all fire inside the one specific user control.
I would like the actual main form and any other user control on the form to ignore the keypress. Is this possible?
You can do this by ensuring that your control always has focus. An easy way is in the Control.LostFocus event set the focus back. The UserControls base class isn't great about recieving focus, so instead you may want to pick a control within your user control to always have focus and receive events.

Use right-click with Windows Forms Button

I am writing an app where the user should be able to alter the action of a button.
The user should right-click a button, and choose an option from a pop-up context menu. Once the choice has been made the button will perform a different action when the user uses a normal click.
I've already gotten the "Click" event working for a normal button click, however the "MouseClick" event handler isn't working correctly.
The "MouseClick" event gets activated on regular left-clicks, but never get's called for right-click.
Is there some default event handling being performed that is ignoring that right-click?
I'm sorry to say that this would be a serious UI blooper. Perhaps it would make more sense to add a small combobox next to the button.
Perhaps something like this?
http://www.codeproject.com/KB/buttons/SplitButton.aspx
If you want to display a context menu with actions to choose from it should be enough to assign a ContextMenuStrip to the ContextMenuStrip property. There is usually no need to manually handle the mouse events for that.
surely it would be better to implement it on the MouseDown event rather than the MouseUp event. i dont understand how this is much different from MouseClick event
edit: Just tried this and it works a treat :)
In Button (and certain other controls), the MouseClick event is only fired for the left button. Refer to MSDN.
If you want to know about the right button, respond to the MouseUp event--though as other posters have pointed out, this isn't a great UI idiom.
Use the Mouse UP event... test that Button.X and Button.Y are within the size of the button otherwise you have moved the mouse away from the button.
Terry G

Categories

Resources