Home > Net >  Online and so on -- -- -- -- -- WPF find dynamically create child controls
Online and so on -- -- -- -- -- WPF find dynamically create child controls

Time:01-23

Dynamic creation has not yet been added to the UI on custom controls, look for less than its child controls, using VisualTreeHelper doesn't work, can't find, what method can dynamically created controls to find the child controls?
Such as
C=new UserControl1 MyUserControl () {Content=new Grid ()};
Var f=c.I sInitialized;//is true
Var BTN=FindChildren (c). ToList ();//count=0 means not found, it is using VisualTreeHelper to find the
The Debug. Print (" name: "+ b.N ame);
B.N ame="TTT";
}

With FindName (... ) there is no effect, (looking for custom controls child controls, and is in the child controls without InitializeComponent part),
Such as:
Public UserControl1 ()
{
//InitializeComponent ();
The Grid GridContainer=new Grid () {Name="yy9999"};
Var n=GridContainer. FindName (" yy9999 ");//is NULL

Or System. Windows. NameScope. SetNameScope (GridContainer, the new System. Windows. NameScope ());
RegisterName (" GGG ", GridContainer);//an error,,,, because there is no InitializeComponent ()?

CodePudding user response:

That is://InitializeComponent ();=> InitializeComponent ();
In addition, there is no MVVM to write WPF, you will be very tired, feeling a lot of things are bad
  •  Tags:  
  • C#
  • Related