首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >公开UserControls ContentTemplate

公开UserControls ContentTemplate
EN

Stack Overflow用户
提问于 2017-11-27 12:06:50
回答 1查看 335关注 0票数 2

我正试着让我的拳头UserControl在C#。这是一个TabControll,有一些生活质量的改善。目标是能够在各种项目中使用它,因此它必须尽可能通用。

到目前为止,我已经通过一个ItemSource公开了DependencyProperty。但是我对如何对ContentTemplate属性做同样的事情感到厌烦。

到目前为止,下面是我的代码示例:

XAML:

代码语言:javascript
复制
<UserControl ...>
    <UserControl.Resources>
        <!-- some styles and templates -->
    </UserControl.Resources>
    <TabControl ItemsSource="{Binding ItemsSource}" SelectedIndex="{Binding selectedIndex}"
            Style="{StaticResource FixatedTabControl}" ItemTemplateSelector="{StaticResource myDataTemplateSelector}"/>
</UserControl>

背后的代码:

代码语言:javascript
复制
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); }
        }
}

我可以像这样使用DynamicTabControl:

代码语言:javascript
复制
<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>

但是,如何才能更改/添加UserControl的contenTemplate的TabControl呢?我想让它表现得像这样:

代码语言:javascript
复制
<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>

我还在学习,所以请帮帮我。提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-27 13:01:58

UserControl添加另一个依赖项属性

代码语言:javascript
复制
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绑定到它:

代码语言:javascript
复制
<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>

然后,可以在窗口的XAML标记中设置此属性:

代码语言:javascript
复制
<foo:DynamicTabControl x:Name="testTabContr" ItemsSource="{Binding data}">
    <foo:DynamicTabControl.TabContentTemplate>
        <DataTemplate>
            <TextBox Text="{Binding someData}"/>
        </DataTemplate>
    </foo:DynamicTabControl.TabContentTemplate>
</foo:DynamicTabControl>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47510640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档