为以编程方式创建的控件创建事件处理程序 [英] Create an event handler for control created programmatically

查看:89
本文介绍了为以编程方式创建的控件创建事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF应用程序,我想要的是,如果从A组中选择了一个特定的单选按钮,则程序将创建第二组单选按钮,称为B组,同时禁用一个文本框。现在,我想创建一个事件,这样,如果用户从B组中选择选项3,则该文本框将变为启用状态,而当未选中该选项时,它将再次禁用。现在,我可以通过编程方式进行工作,但是当我尝试创建事件处理程序时,我引用的是不存在的控件,它将无法构建。我尝试使用 this.textBox.IsEnabled = true / false; 启用/禁用文本框。因此,只是要确保实际问题明确。如何启用/禁用在构建/运行时不存在的文本框?

I am working on a WPF application and what I want is if a specific Radio Button from group A is selected the program creates a second set of Radio Buttons call them group B and at the same time a disabled textbox. Now I want to create an event so if the user Selects option 3 from Group B the text box becomes enabled and when it is unselected it disables again. Now I have the working programmatically but when I try to create the event handlers I am referencing a non-existent control and it won't build. I am trying to use this.textBox.IsEnabled = true/false; to enable/disable the textbox. So just to make sure the actual question is clear. How can I enable/disable a textbox which doesn't exist at build/runtime?

我的活动:

private void AllowRegion(object sender, RoutedEventArgs e)
        {
            this.SpecRegionText.IsEnabled = true;
        }
private void DisallowRegion(object sender, RoutedEventArgs e)
        {
            this.SpecRegionText.IsEnabled = false;
        }

创建控件:

private void AddSpecificControls()
        {
            Grid grid = (Grid)this.SpecsGrid;
            RadioButton recruitmentEnabled = (RadioButton)this.RecruitmentEnabled;
            if ((bool)recruitmentEnabled.IsChecked)
            {
                RadioButton newNations = new RadioButton();
                newNations.Margin = new Thickness(10, 10, 0, 0);
                newNations.HorizontalAlignment = HorizontalAlignment.Left;
                newNations.Name = "NewNations";
                newNations.GroupName = "RecruitType";
                newNations.Content = "New Nations";
                newNations.Width = 124;
                grid.Children.Add(newNations);
                RadioButton reFound = new RadioButton();
                reFound.Margin = new Thickness(10, 30, 0, 0);
                reFound.HorizontalAlignment = HorizontalAlignment.Left;
                reFound.Name = "Refound";
                reFound.GroupName = "RecruitType";
                reFound.Content = "Refounded Nations";
                reFound.Width = 124;
                grid.Children.Add(reFound);
                RadioButton specRegionRadio = new RadioButton();
                specRegionRadio.Margin = new Thickness(10, 50, 0, 0);
                specRegionRadio.HorizontalAlignment = HorizontalAlignment.Left;
                specRegionRadio.VerticalAlignment = VerticalAlignment.Top;
                specRegionRadio.Name = "SpecRegionRadio";
                specRegionRadio.GroupName = "RecruitType";
                specRegionRadio.Content = "Specific Region";
                specRegionRadio.Width = 124;
                specRegionRadio.Height = 23;
                specRegionRadio.Checked += new RoutedEventHandler(AllowRegion);
                specRegionRadio.Unchecked += new RoutedEventHandler(DisallowRegion);
                grid.Children.Add(specRegionRadio);
                TextBox specRegionText = new TextBox();
                specRegionText.Margin = new Thickness(139, 50, 0, 0);
                specRegionText.HorizontalAlignment = HorizontalAlignment.Left;
                specRegionText.VerticalAlignment = VerticalAlignment.Top;
                specRegionText.Name = "SpecRegionText";
                specRegionText.Text = "Region";
                specRegionText.Width = 120;
                specRegionText.Height = 23;
                specRegionText.IsEnabled = false;
                grid.Children.Add(specRegionText);
            }
        }


推荐答案

您可以使用lambda挂接委托,但是为此您必须在radioButton之前声明文本框

You can hook delegate inline using lambda but for that you have to declare textBox before radioButton.

TextBox specRegionText = new TextBox();
RadioButton specRegionRadio = new RadioButton();
specRegionRadio.Checked += (s,e) => specRegionText.IsEnabled = true;
specRegionRadio.Unchecked += (s,e) => specRegionText.IsEnabled = false;

这篇关于为以编程方式创建的控件创建事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆