从重复的选项卡访问控件 [英] Accessing Controls from Duplicated Tabs
问题描述
您好,
我正在使用WPF开发一个需要使用动态标签的应用程序。为列表中的每个项目创建一个TabItem,并从隐藏的"tabToDuplicate"复制一个TabItem。并添加到TabControl:
I'm developing an application with WPF that requires the use of dynamic tabs. A TabItem is created for each item in a list and duplicated from a hidden "tabToDuplicate" and added to TabControl:
<TabItem Header=" " Name="tabToDuplicate" Visibility="Hidden">
<Grid Background="#FFE5E5E5">
<DataGrid Name="BookData" Margin="10,10,409,25" RenderTransformOrigin="-0.63,0.07" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"></DataGrid>
<Button Content="Button" HorizontalAlignment="Left" Margin="304,59,0,0" VerticalAlignment="Top" Width="80" Height="23"/>
</Grid>
</TabItem>
private void Window_Initialized(object sender, EventArgs e)
{
foreach (string i in TabList)
TabControl.Items.Insert(TabControl.Items.Count-1, NewTabItem(i));
}
private TabItem NewTabItem(string Name)
{
TabItem NewTab = CloneObject(tabToDuplicate);
NewTab.Name = "tab" + Name;
NewTab.Header = Name;
NewTab.Visibility = Visibility.Visible;
return NewTab;
}
TabItem已成功添加并正确复制。我不确定如何访问在新的重复TabItem中创建的子控件以获取/设置属性等。我可以提供一些建议。
The TabItem is successfully added and duplicated correctly. I'm not sure how to access child controls created within the new duplicated TabItem to get/set properties etc. Please can I have some advice.
谢谢。
推荐答案
WPF答案是你没有。  ;所有属性都应该在XAML中定义,并使用数据绑定来设置属性。
The WPF answer is you don't. All properties should be defined in XAML and use data binding to set properties.
此外,由于TabControl是从ItemsControl派生的,因此您将使用的过程是拥有定义数据的对象集合在每个选项卡后面(通常是一个ObservableCollection,这样当你需要一个新标签时,你只需在代码中添加一个新对象
,其中包含所需的数据到集合中,WPF将创建新的TabItem)。
Also since the TabControl is derived from ItemsControl the process you would use is to have a collection of objects that define the data behind each tab (usually an ObservableCollection so that when you need a new tab you simply in code add a new object with the needed data to the collection and WPF will create the new TabItem).
这篇关于从重复的选项卡访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!