简体   繁体   English

公开一个UserControls ContentTemplate

[英]Expose a UserControls ContentTemplate

I'm trying to make my fist UserControl in C#. 我正在尝试在C#中创建我的拳头UserControl。 It is a TabControll with some quality of life improvements. 它是一个TabControll,生活质量有所改善。 The goal is to be able to use it in various projects, so it has to be as generic as possible. 目标是能够在各种项目中使用它,因此它必须尽可能通用。

So far I have exposed the ItemSource through a DependencyProperty. 到目前为止,我已经通过DependencyProperty公开了ItemSource。 But I'm suck with how to do the same with the ContentTemplate Property. 但是我很讨厌如何对ContentTemplate属性进行相同的处理。

Here's an example of my code so far: 到目前为止,这是我的代码示例:

XAML: XAML:

<UserControl ...>
    <UserControl.Resources>
        <!-- some styles and templates -->
    </UserControl.Resources>
    <TabControl ItemsSource="{Binding ItemsSource}" SelectedIndex="{Binding selectedIndex}"
            Style="{StaticResource FixatedTabControl}" ItemTemplateSelector="{StaticResource myDataTemplateSelector}"/>
</UserControl>

The code behind: 后面的代码:

public partial class DynamicTabControl : UserControl, INotifyPropertyChanged
{
    public DynamicTabControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable<ITabItem>), typeof(DynamicTabControl));
        public IEnumerable<ITabItem> ItemsSource
        {
            get { return (IEnumerable<ITabItem>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
}

I can use the DynamicTabControl like so: 我可以像这样使用DynamicTabControl:

<Window x:Class="Demo.MainWindow"
        ...            
        xmlns:local="clr-namespace:Demo"
        xmlns:foo="clr-namespace:DynamicTabUserControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <foo:DynamicTabControl x:Name="testTabContr" ItemsSource="{Binding data}">

        </foo:DynamicTabControl>
    </Grid>
</Window>

But how can I make it possible to alter/add the contenTemplate of the UserControl's TabControl? 但是,如何才能更改/添加UserControl的TabControl的contenTemplate? I would like to get it to behave like such: 我想让它表现得像这样:

<Window x:Class="Demo.MainWindow"
            ...            
            xmlns:local="clr-namespace:Demo"
            xmlns:foo="clr-namespace:DynamicTabUserControl"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <foo:DynamicTabControl x:Name="testTabContr" ItemsSource="{Binding data}">
               <foo:DynamicTabControl.TabControl.ContentTemplate>
                   <DataTemplate>
                      <TextBox Text="{Binding someData}"/>
                   </DataTemplate>
               </foo:DynamicTabControl.TabControl.ContentTemplate>
            </foo:DynamicTabControl>
        </Grid>
    </Window>

I'm still learning, so please help me out. 我仍在学习,所以请帮帮我。 Thank you in advance. 先感谢您。

Add another dependency property to the UserControl : UserControl添加另一个依赖项属性:

public partial class DynamicTabControl : UserControl, INotifyPropertyChanged
{
    public DynamicTabControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable<ITabItem>), typeof(DynamicTabControl));
    public IEnumerable<ITabItem> ItemsSource
    {
        get { return (IEnumerable<ITabItem>)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty TabContentTemplateProperty =
        DependencyProperty.Register("TabContentTemplate", typeof(DataTemplate), typeof(DynamicTabControl));

    public DataTemplate TabContentTemplate
    {
        get { return (DataTemplate)GetValue(TabContentTemplateProperty); }
        set { SetValue(TabContentTemplateProperty, value); }
    }
}

...and bind to it: ...并绑定到它:

<UserControl>
    <UserControl.Resources>
        <!-- some styles and templates -->
    </UserControl.Resources>
    <TabControl ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
                Style="{StaticResource FixatedTabControl}"
                ContentTemplate="{Binding TabContentTemplate, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</UserControl>

You can then set this property in the XAML markup of the window: 然后,您可以在窗口的XAML标记中设置此属性:

<foo:DynamicTabControl x:Name="testTabContr" ItemsSource="{Binding data}">
    <foo:DynamicTabControl.TabContentTemplate>
        <DataTemplate>
            <TextBox Text="{Binding someData}"/>
        </DataTemplate>
    </foo:DynamicTabControl.TabContentTemplate>
</foo:DynamicTabControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM