WPF - 向动态添加的 Tabitem 添加动态控件? [英] WPF - Adding dynamic controls to dynamically added Tabitem?
问题描述
我在运行时(在 C# 中)动态地将 Tabitem 添加到 Tab 控件并且工作正常,但是我如何才能将控件动态添加到新的 Tabitem 中?Tabitem 需要是动态的,因为它们取决于从数据库中读取的数据行数.每个 Tabitem 的布局都是相同的.谢谢
I am dynamically adding Tabitems to a Tab Control at runtime (in C#) and that works OK, but how can I then dynamically add controls to the new Tabitems? The Tabitems need to be dynamic because they depends on how many rows of data are read from a database. The layout of each Tabitem will be identical. Thanks
推荐答案
如果每个 TabItem
将具有相同的布局,我将简单地创建一个 UserControl
包含什么您需要从布局和控制状态,然后将其放置在 TabItem.Content
属性中.
If each TabItem
is going to have the same layout I would simply create a UserControl
which encompasses what you need from a layout and control stance and then place that within the TabItem.Content
property.
然后您可以通过对象表示将数据传递给 TabItem.DataContext
属性以启动和使用绑定.
You could then pass the data via object representation to the TabItem.DataContext
property to initiate and make use of binding.
TabItem item = new TabItem();
item.Content = new CustomUserControl();
item.DataContext = data; //where data is the data that
//comes from the database
//being represented in object form
这篇关于WPF - 向动态添加的 Tabitem 添加动态控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!