2014-02-23 189 views
0

我正在WPF應用程序上工作,我想要的是如果選擇了組A中的特定單選按鈕,程序創建第二組單選按鈕將其稱爲組B,時間禁用文本框。現在我想創建一個事件,所以如果用戶從組B中選擇選項3,則文本框變爲啓用狀態,並且當它被取消選擇時,它將再次禁用。現在我以編程方式工作,但是當我嘗試創建事件處理程序時,我引用了一個不存在的控件,並且它不會生成。我正在嘗試使用this.textBox.IsEnabled = true/false;來啓用/禁用文本框。所以只是爲了確保實際問題清楚。如何啓用/禁用在構建/運行時不存在的文本框?爲程序創建的控件創建事件處理程序

我的活動:

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

回答

1
可以 鉤委託直列使用lambda但你RADIOBUTTON之前必須 申報的textBox

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