开发者

c# wpf how to reference a dynamically added control

Just starting out with C#, so this may be overly simple that I keep overlooking it... In the main window I have a stackpanel, named targetDocArea, that will hold controls. Based on user input, controls appear in the panel like so:

var htmlView = new System.Windows.Controls.WebBrowser();
htmlView.MinHeight = 200;
htmlView.Height = deskHeight - 225;
htmlView.Name = "targetDocControl";
htmlView.Navigate(dlg.FileName);
this.targetDocArea.Children.Add(htmlView);

Now I have another function that needs to interact with that control - and that's where I'm a bit lost. I would think there would be some index I could use to refere开发者_开发百科nce the children of the panel or directly using the name.

I've been reading about "this.registerName" but I'm not sure if that is correct way to approach this problem.

Any guidance would be greatly appreciated - and I wouldn't mind changing from a stackpanel to something more suited that would allow this interactivity.

Thanks.


You should store the control in a field in your class, like this:

private WebBrowser htmlView;

You can then use this field in any function.

If you need to store a number of copies of the control, you can use a List<WebBrowser> field, like this:

private List<WebBrowser> htmlViews = new List<WebBrowser>();

//Elsewhere:
htmlViews.Add(something);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜