如何将TabItems添加到自定义UserControl的TabControl中?

时间:2013-11-11 15:44:05

标签: wpf xaml user-controls tabcontrol tabitem

好的,我正在阅读this,它显示了如何在当前XAML中向TabItem添加自定义TabControl,但如果我想添加{{1在XAML中自定义TabItems

所以我创建了自定义TabControl TabControl

UserControl

然后,我想在<UserControl x:Class="myLibrary.MyTabControl"> <DockPanel LastChildFill="True"> <Grid DockPanel.Dock="Bottom"/>> </DockPanel> <TabControl x:Name=tc"> <TabControl.LayoutTransform> <!-- Allows to zoom the control's content using the slider --> <ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding ElementName=uiScaleSlider,Path=Value}" ScaleY="{Binding ElementName=uiScaleSlider,Path=Value}"/> </TabControl.LayoutTransform> </TabControl> </UserControl> TabItems添加静态MyUserControl,如下所示

UserControl

而不是使用默认的WPF <UserControl x:Class="MyLibrary.Forms.MyTabForm" xmlns:Utilities="clr-namespace:myLibrary;assembly=myLibrary"> <Utilities:MyTabControl DockPanel.Dock="Top"> <tc> <tc.Items> <TabItem Header="Tab 0"/> <TabItem Header="Tab 1"/> </tc.Items> </tc> </Utilities:MyTabControl> </UserControl>

TabControl

2 个答案:

答案 0 :(得分:0)

您需要在DependencyProperty添加UserControl,以便用户BindTabControl.Items项目添加到控件中的public static readonly DependencyProperty ItemsProperty = DependencyProperty. Register("Items", typeof(ItemCollection), typeof(MyTabControl)); public ItemCollection Items { get { return (ItemCollection)GetValue(ItemsProperty); } set { SetValue(ItemsProperty, value); } } 属性中:

Bind

然后你可以RelativeSource Binding使用这样的<UserControl x:Class="myLibrary.MyTabControl"> <DockPanel LastChildFill="True"> <Grid DockPanel.Dock="Bottom"/>> </DockPanel> <TabControl x:Name=tc" Items="{Binding RelativeSource={RelativeSource AncestorType={x:Type YourXmlNamespace:MyTabControl}}}"> <TabControl.LayoutTransform> <!-- Allows to zoom the control's content using the slider --> <ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding ElementName=uiScaleSlider,Path=Value}" ScaleY="{Binding ElementName=uiScaleSlider,Path=Value}"/> </TabControl.LayoutTransform> </TabControl> </UserControl> <Utilities:MyTabControl DockPanel.Dock="Top" Items="{Binding SomeItemCollection}" /> 来控制此属性:

{{1}}

然后你可以像这样使用它:

{{1}}

答案 1 :(得分:0)

  1. 将IEnumerable的依赖项属性类型添加到User Control(例如UserControlItemsSource
  2. 将制表符控件的属性ItemsSource绑定到此依赖项属性(ItemsSource="{Binding UserControlItemsSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}"
  3. 使用用户控件时 - 绑定用户控件的依赖项属性(UserControlItemsSource)。