2

I'm implementing the Tokenizing Control as per this website: http://blog.pixelingene.com/2010/10/tokenizing-control-convert-text-to-tokens/

And I'm now trying to tidy things up and make it more MVVM friendly. So I've moved the DataTemplate from the Windows Resource into Themes/Generic.xaml file:

<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>

I've update the control to load the generic style in the constructor:

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

        TextChanged += OnTokenTextChanged;
    }

However the TokenTemplate property is always null and hence no style gets applied.

What am I missing here?

1 Answer 1

2

Try to put the NameTokenTemplate data template in the generic.xaml's resource dictionary, just before the <Style TargetType="{x:Type local:TokenizingControl}"> line.

Be sure that you are making reference to the generic.xaml in the 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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Raul, that worked - but why do I have to reference the generic.xaml in my app resources? I have done other custom controls and never had to add it to the resources. Is it because its a datatemplate?
The App.xaml's resources are the application global one. That means all components can access them (if not overwritten). If you to create an user control instances you can do it in any place of you app, but if you want access brushes, data templates, styles,... you should define them in resource dictionaries, and the App.xaml's one is the app gobal one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.