2014-01-10 45 views
1

我執行令牌化控制按本網站: http://blog.pixelingene.com/2010/10/tokenizing-control-convert-text-to-tokens/在Generic.xaml使用的DataTemplate WPF的自定義控件

而現在我想整理東西,使之更加友好MVVM。 所以我從Windows資源移動到DataTemplate的主題/ 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屬性始終爲空,因此不會應用樣式。

我在這裏錯過了什麼?

回答

1

嘗試將NameTokenTemplate數據模板放在generic.xaml的資源字典中,位於<Style TargetType="{x:Type local:TokenizingControl}">行之前。

確保您在App.xaml使參考generic.xaml

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

感謝勞爾,那工作 - 但爲什麼我要引用generic.xaml我的應用程序的資源?我已經完成了其他自定義控件,並且不必將其添加到資源中。是因爲它的數據模板嗎? – fergs

+0

App.xaml的資源是應用程序全局資源。這意味着所有組件都可以訪問它們(如果不覆蓋)。如果你創建一個用戶控件實例,你可以在你的應用程序的任何地方執行它,但是如果你想要訪問畫筆,數據模板,樣式等,你應該在資源字典中定義它們,而App.xaml的是應用程序gobal之一。 –