我如何处理c#wpf自定义控件中的事件

时间:2021-12-30 04:50:43

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#?

我刚开始在wpf中探索自定义控件。我通常是vb.net开发人员。在vb.net中,右上角的组合框中的代码文件中有一个事件列表。即使组合框在那里,C#中也没有事件。我知道如何覆盖c#中的事件,但签名不一样,这与处理事件不同。在C#的wpf自定义控件中处理事件的正确方法是什么?

1 个解决方案

#1


1  

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 ...):

您可以在设计器的属性工具箱,事件选项卡中查看公开事件的列表。或者,点击。在控件实例和intellisense中查找具有闪电图标的成员。例如,使用名为tb的TextBox(在自定义控件和开箱即用控件中处理事件之间没有区别......):

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());

#1


1  

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 ...):

您可以在设计器的属性工具箱,事件选项卡中查看公开事件的列表。或者,点击。在控件实例和intellisense中查找具有闪电图标的成员。例如,使用名为tb的TextBox(在自定义控件和开箱即用控件中处理事件之间没有区别......):

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());