views:

61

answers:

2

I have just started to explore custom controls in wpf. I am typically a vb.net developer. In vb.net there are a list of events in the code file in the upper right combo box. Even though the combo box is there, the events are not there in C#. I know how to override the events in c# but the signature is not the same and this is not the same thing as handling the events. What is the proper way to handle events in wpf custom controls for C#?

A: 

Is it really a requirment to create a Custom Control? Because lot of Control can be changed with out creating a custom control.

1- If you are just changing look and Feel so you don't need a Custom Control , just read about Templates in Wpf.

2- If you are adding a new property to your custom control before writing a custom control, try to read about Attached Properties.

saurabh
Thank you for your answer. I am really creating a user control but with no visual components. I want my user control to be able to be templated. So for this reason I think custom control is appropriate.
Chris Perry
+1  A: 

You can see a list of the exposed events within the property toolbox, events tab, in the designer. Alternatively, hit . on the control instance and in intellisense look for members with the lightning bolt icon. For example, using a TextBox called tb (there's no difference between handling events in custom controls vs. out-of-the-box controls ...):

TextBox tb = new TextBox();            
this.Grid1.Children.Add(tb);
tb.KeyDown += new KeyEventHandler(tb_KeyDown);

With a handler like so:

void tb_KeyDown(object sender, KeyEventArgs e)
{
        MessageBox.Show(e.Key.ToString());
}

Or:

TextBox tb = new TextBox();            
this.Grid1.Children.Add(tb);
tb.KeyDown += (o, e) => MessageBox.Show(e.Key.ToString());
Richard Hein
Thank you for your answer. However, I am designing the custom control. I am concerned with how to handle the events when I am designing the control. There is no UI designer for custom controls as they are meant to be designerless controls. Templates are used to give them the visual appearance. I want to handle events in the code file.
Chris Perry