we have a problem when trying to create events for a custom control in WPF. We have our code like this:
public static readonly RoutedEvent KeyPressedEvent = EventManager.RegisterRoutedEvent( "keyPressed", RoutingStrategy.Bubble, typeof(KeyEventHandler), typeof(Keyboard));public event KeyEventHandler keyPressed { add { AddHandler(KeyPressedEvent, value); } remove { RemoveHandler(KeyPressedEvent, value); } } void btnAlphaClick(object sender, RoutedEventArgs e) { var btn = (Button)sender; Key key = (Key)Enum.Parse(typeof(Key), btn.Content.ToString().ToUpper()); PresentationSource source = null; foreach (PresentationSource s in PresentationSource.CurrentSources) { source = s; } RaiseEvent(new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice, source,0,key));The control is an on screen keyboard, and we basically need to pass out to the KeyPressedEventArgs to the subscribers to the event detailing what key was pressed (we can't find much that helps us with this in WPF, only winforms).
Any help, greatly appreciated!
Step 1: Add Event Handler to OK and Cancel Button
Add the public property in the UserControl1.xaml.cs file to share the value of the textbox with the host
Declare the Events for Ok and Cancel Buttons which can be subscribed by Windows Form.
Now add the code to the event handler so that we can raise the event to host also.
Step 2: Handle the WPF Control Event in Windows Form
Add Handler to
OKClickandCancelClickEvents just after creating the instance of the user controlWrite code in the handler method. Here I user the
UserNameproperty in the OK button handler so show the how to share the values also.Reference:
http://a2zdotnet.com/View.aspx?Id=79