I know how to create a dynamic control in c#:
TextBlock tb = new TextBlock();
tb.Text = "This is a new textblock";
But how would I reference this newly created control through code?
I browsed the net for a solution, and came across this code:
TextBlock tb = (TextBlock)this.FindName("TB");
tb.Text = "Text property changed";
Every time I create a new control with a name I get an exception:
TextBlock tb = new TextBlock();
tb.Text = "This is a new textblock";
tb.Name = "TB";
“The parameter is incorrect.”
What am I doing wrong? Any help would be greatly appreciated.
Thanks in advance.
The Exception “The parameter is incorrect.” may be occurring because of the duplicate names of the controls created.
For the dynamic control part : you must be adding that control to some Grid or Stackpanel or something. you can reference that dynamic control by getting the content or children of the parent control.
Like :
//to reference :
Hope, it might help.
(Note: I have written this code directly here and not copied from VS, so please check syntax and spellings.)