简体   繁体   English

如何处理在代码背后实例化的控件的事件

[英]How to handle events of a control instantiated in codebehind

I have a WPF UserControl in which I am creating a TextBox in the code-behind, rather than in the xaml. 我有一个WPF UserControl,我在其中创建一个TextBox,而不是在xaml中。 The TextBox shows and works just fine, and I can access its methods and do things like getting the text from it. TextBox可以正常显示和工作,我可以访问其方法并执行诸如从中获取文本的操作。

Now, I want to handle that TextBox's TextChanged event. 现在,我要处理该TextBox的TextChanged事件。 I can't use the OnTextChanged method, as I'm trying to handle the child of the UserControl, rather than the UserControl itself. 我无法使用OnTextChanged方法,因为我正在尝试处理UserControl的子级,而不是UserControl本身。 Because the TextBox was created dynamically, rather than in the xaml, I can't use the xaml Click to specify the event's handler in my UserControl code-behind. 因为TextBox是动态创建的,而不是在xaml中动态创建的,所以我不能使用xaml Click在后面的UserControl代码中指定事件的处理程序。

I've finally tried using the TextBox.AddHandler method of the TextBox, and that appears to be promising, if I can get it to work. 我终于尝试使用TextBox的TextBox.AddHandler方法,并且如果我能使它工作的话,那似乎很有希望。

Here's some snippets from my code: 这是我的代码中的一些片段:

I included this line in order to allow my code to recognize the TextBoxBase event group: 我包含此行是为了使我的代码能够识别TextBoxBase事件组:

using System.Windows.Controls.Primitives;

This is the class definition: 这是类的定义:

    public partial class UniversalControl : UserControl
    {

This is the line within the class where I instantiate the TextBox: 这是类中实例化TextBox的行:

        private TextBox theText = new TextBox();

This is a method that is called to set up some controls, of which the TextBox is one: 这是用来设置一些控件的方法,其中TextBox是其中的一个:

        private  void InitializeControl(ControlTypes Type)
        {

And, here is the line within that method that causes the error. 并且,这是该方法中导致错误的行。 I'm trying here to set the event handler to a method I define in the UniversalControl class. 我在这里尝试将事件处理程序设置为我在UniversalControl类中定义的方法。

            theText.AddHandler(TextBoxBase.TextChanged, new RoutedEventHandler(OnTextBoxTextChanged));
        }

This is the event handler itself: 这是事件处理程序本身:

        private void thePanel_TextChanged(object sender, TextChangedEventArgs e)
        {
            MessageBox.Show("The text changed.");
        }

The error that is thrown is: 引发的错误是:

CS0120  An object reference is required for the non-static field, method, or property, 'TextBoxBase.TextChanged'

Why am I getting this error? 为什么会出现此错误? I've searched for hours and found lots of people encountering this error, but none with my particular set of circumstances. 我搜索了几个小时,发现很多人遇到此错误,但是在我的特定情况下都没有。

Please provide solutions that do not require xaml other than in my UserControl. 请提供不需要xaml的解决方案(我的UserControl中不需要)。 Remember, the TextBox is not defined in xaml. 请记住,文本框未在xaml中定义。 I'm also not well-enough versed in xaml to be building complicated styles or templates. 我也不熟悉xaml来构建复杂的样式或模板。

Perhaps there's another approach I could take. 也许我可以采取另一种方法。 My desire is to create the TextBox dynamically and make it appear only when needed. 我的愿望是动态创建TextBox,并使它仅在需要时显示。 Other controls may appear in its place in other instantiations of my UserControl. 其他控件可能会在我的UserControl的其他实例中代替它出现。

You can do this two ways. 您可以通过两种方式执行此操作。 They both do the same thing . 他们俩都做同样的事情

TextChanged is an event , an instance member of TextBox . TextChanged是一个event ,是TextBox的实例成员。 Assign a handler to it like this. 这样分配一个处理程序。 You don't need new RoutedEventHandler() . 您不需要new RoutedEventHandler() This is conventional .NET event handling, not specific to XAML. 这是常规的.NET事件处理,不特定于XAML。 I recommend this approach. 我推荐这种方法。

theText.TextChanged += OnTextBoxTextChanged;

The approach you're trying to use is a XAML thing. 您尝试使用的方法是XAML。 The static routed event property is named, by convention, TextBoxBase.TextChangedEvent . 按照惯例,静态路由事件属性命名为TextBoxBase.TextChangedEvent That's used by XAML for adding event handlers with EventSetter and so on. XAML使用它来通过EventSetter等添加事件处理程序。 No need for you to bother with it, but if you're a glutton for punishment, that would look like this: 无需您费心,但是如果您是贪食的惩罚者,则如下所示:

theText.AddHandler(TextBoxBase.TextChangedEvent, 
    new TextChangedEventHandler(OnTextBoxTextChanged));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM