Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何将TabItems添加到自定义UserControl的TabControl中?_Wpf_Xaml_User Controls_Tabcontrol_Tabitem - Fatal编程技术网

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

Wpf 如何将TabItems添加到自定义UserControl的TabControl中?,wpf,xaml,user-controls,tabcontrol,tabitem,Wpf,Xaml,User Controls,Tabcontrol,Tabitem,好的,我正在阅读,它展示了如何在当前XAML中将自定义TabItem添加到TabControl中,但是如果我想在XAML中将TabItems添加到自定义TabControl中呢 因此,我创建了我的自定义TabControlUserControl <UserControl x:Class="myLibrary.MyTabControl"> <DockPanel LastChildFill="True"> <Grid DockPanel.Dock

好的,我正在阅读,它展示了如何在当前XAML中将自定义
TabItem
添加到
TabControl
中,但是如果我想在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>
不使用默认的WPF
TabControl

<TabControl Name="tabControl1" Margin="0, 10, 0, 0"  DockPanel.Dock="Top">
    <TabItem Header="Tab 0 (0)" Name="tabItem0">
        <Grid Name="tabItem0Grid" />
    </TabItem>
    <TabItem Header="Tab 1 (0)" Name="tabItem1">
        <Grid Name="tabItem1Grid" />
    </TabItem>


您需要在
UserControl
中添加一个
dependencProperty
,使用户能够
项绑定到
选项卡控件。控件中的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); }
} 
然后,您可以使用
相对资源绑定
绑定到控件内的此属性,如下所示:

<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}" />

>
  • 将IEnumerable的依赖项属性类型添加到
    用户控件中(例如
    UserControlItemsSource
  • 将选项卡控件的属性
    ItemsSource
    绑定到此依赖项属性(
    ItemsSource=“{Binding UserControlItemsSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}}”
  • 使用用户控件时,请绑定用户控件的依赖项属性(
    UserControlItemsSource

  • 你不能用tc.TabItem代替tc.Items吗?@sexta13我无法让它识别
    tc
    部分。我添加了
    x:FieldModifier=“public”
    ,但这也不起作用。如果我不想使用绑定呢?创建从
    TabControl
    继承的类是否更好?我有一个用于缩放的
    滑块
    ,我想在
    选项卡内容
    下面添加它。我想这需要我更改TabControl模板。我个人还没有尝试过,但这对我来说肯定是有意义的。如果您将控件中的
    UserControl
    替换为
    TabControl
    ,那么我可以想象您可以做您想做的事情。
    <Utilities:MyTabControl DockPanel.Dock="Top" Items="{Binding SomeItemCollection}" />