Wpf 如何将TabItems添加到自定义UserControl的TabControl中?
好的,我正在阅读,它展示了如何在当前XAML中将自定义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
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>
不使用默认的WPFTabControl
:
<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}" />