2010-07-29 109 views
2

如何使這個模板放在資源:RadGridView模板資源

<telerikGridView:RadGridView x:Name="MyGridView" ItemsSource="{Binding MySource}" AutoGenerateColumns="False"> 
    <telerik:RadGridView.Columns> 
     <telerikGridView:GridViewColumn Header="Id"> 
      <telerikGridView:GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding ID}" /> 
       </DataTemplate> 
      </telerikGridView:GridViewColumn.CellTemplate> 
     </telerikGridView:GridViewColumn> 
     <telerikGridView:GridViewColumn Header="Name"> 
      <telerikGridView:GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding NAME}" /> 
       </DataTemplate> 
      </telerikGridView:GridViewColumn.CellTemplate> 
     </telerikGridView:GridViewColumn> 
    </telerik:RadGridView.Columns> 
</telerikGridView:RadGridView> 

我有2個有相同tamplate其他網格。

回答

1

創建字典文件,並在App.xaml中添加其參考

<Application.Resources> 
    <ResourceDictionary> 
       <ResourceDictionary.MergedDictionaries> 
        <ResourceDictionary Source="Resourses/DataTemplates.xaml"/> 
       </ResourceDictionary.MergedDictionaries> 
      </ResourceDictionary> 
    </Application.Resources> 

資源文件的內容...

<ResourceDictionary 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
     mc:Ignorable="d"> 
      <DataTemplate x:Key="GridIDTemplate"> 
      <TextBlock Text="{Binding ID}" /> 
     </DataTemplate> 
     <DataTemplate x:Key="GridNameTemplate"> 
      <TextBlock Text="{Binding Name}" /> 
     </DataTemplate> 
    </ResourceDictionary> 

而且在名字的文件引用數據模板

<telerikGridView:RadGridView x:Name="MyGridView" ItemsSource="{Binding MySource}" AutoGenerateColumns="False"> 
     <telerik:RadGridView.Columns> 
      <telerikGridView:GridViewColumn Header="Id" CellTemplate="{StaticResource GridIDTemplate}" /> 
      <telerikGridView:GridViewColumn Header="Name" CellTemplate="{StaticResource GridNameTemplate}"> 
      </telerik:RadGridView.Columns> 
    </telerikGridView:RadGridView>