在Generic.xaml中使用DataTemplate进行WPF自定义控件

时间:2014-01-10 17:49:12

标签: c# wpf xaml

我正在根据此网站实施令牌控制: http://blog.pixelingene.com/2010/10/tokenizing-control-convert-text-to-tokens/

我现在正在尝试整理一些东西并使其更加友好。 所以我已将DataTemplate从Windows资源移动到Themes / Generic.xaml文件中:     

<Style TargetType="{x:Type local:TokenizingControl}">
    <Style.Resources>
        <DataTemplate x:Key="NameTokenTemplate">
            <DataTemplate.Resources>
                <Storyboard x:Key="OnLoaded1">
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border">
                        <SplineDoubleKeyFrame KeyTime="0" Value="0"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </DataTemplate.Resources>
            <Border x:Name="border" BorderBrush="#FF7E7E7E" BorderThickness="2" CornerRadius="5" Height="Auto" Padding="5,3" Margin="3,0,3,3">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFFFD0A0" Offset="0"/>
                        <GradientStop Color="#FFAB5600" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
                <Grid HorizontalAlignment="Left" Width="Auto">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.21*"/>
                        <ColumnDefinition Width="0.79*"/>
                    </Grid.ColumnDefinitions>
                    <!--<Image HorizontalAlignment="Right" Source="\Images\14-tag.png" Stretch="None" Width="Auto" Grid.Column="0" VerticalAlignment="Center"/>-->
                    <TextBlock TextWrapping="NoWrap" Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="1" Margin="10,0,0,0" FontWeight="Bold"/>
                </Grid>
            </Border>
            <DataTemplate.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard Storyboard="{StaticResource OnLoaded1}"/>
                </EventTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Style.Resources>
    <Setter Property="TokenTemplate" Value="{StaticResource NameTokenTemplate}"></Setter>
</Style>

我更新了控件以在构造函数中加载泛型样式:

    public TokenizingControl()
    {
        // lookless control, get default style from generic.xaml
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TokenizingControl), new FrameworkPropertyMetadata(typeof(TokenizingControl)));

        TextChanged += OnTokenTextChanged;
    }

然而,TokenTemplate属性始终为null,因此不会应用任何样式。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

尝试将NameTokenTemplate数据模板放在generic.xaml的资源字典中,就在<Style TargetType="{x:Type local:TokenizingControl}">行之前。

请务必参考generic.xaml中的App.xaml

    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Views/generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>