好的,我正在阅读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
答案 0 :(得分:0)
您需要在DependencyProperty
添加UserControl
,以便用户Bind
将TabControl.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)
User Control
(例如UserControlItemsSource
)ItemsSource
绑定到此依赖项属性(ItemsSource="{Binding UserControlItemsSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}"
)UserControlItemsSource
)。