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

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

问题描述

我正在开发一个 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 之前声明 textBox.

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天全站免登陆