ResourceDictionary中的自定义TabItem

时间:2012-07-16 10:27:01

标签: c# silverlight xaml custom-controls resourcedictionary

我在ResourceDictionary中创建了一个TabItem并在我的UserControl中使用它,但是定义的内容不会显示,为什么以及如何解决这个问题?

ResourceDic:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">
    <ControlTemplate x:Key="T1" TargetType="sdk:TabItem">
        <sdk:TabItem>
            <sdk:TabItem.Header>
                <StackPanel Orientation="Horizontal">
                    <sdk:Label Content="{TemplateBinding Header}"/>
                    <Button Content="X" Margin="15,0,0,0"/>
                </StackPanel>
            </sdk:TabItem.Header>

            <toolkit:DockPanel MinWidth="600">
                <Grid toolkit:DockPanel.Dock="Left">
                    <sdk:Label Grid.Row="2" Grid.Column="0" Content="Pic:"/>
                    ...
                </Grid>
            </toolkit:DockPanel>

        </sdk:TabItem>
    </ControlTemplate>
</ResourceDictionary>

UserControl:

<UserControl x:Class="PortView" ....>
    <UserControl.Resources>
                <ResourceDictionary Source="PortfolioTemplateDictionary.xaml" />
    </UserControl.Resources>

    <Grid>
        <sdk:TabItem Template="{StaticResource T1}" Header="Page 2"/>
    </Grid  
    ...
</UserControl>

1 个答案:

答案 0 :(得分:1)

我现在使用了本文中解释的方法:TabItem in a separate XAML

<UserControl x:Class="WpfApplication19.Tab1Data"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         >
    <Grid>
        <TextBlock Text="Tab Data" />
    </Grid>
</UserControl>

<TabControl>
    <TabItem Header="Tab 1">
        <tabData:Tab1Data />
    </TabItem>
</TabControl>