2017-04-06 83 views
1

我正在建立一個DataGrid,我想根據當前項目的基礎數據類型在單元格內切換圖像。WPF DataGrid:通過項目的DataType選擇CellTemplateSoure

問題: 是否可以應用這種類型的模板切換? 最好只在xaml中?

的ItemsSource是

ObservableCollection<BaseModel> 

包含類型

IncidentModel : BaseModel 
ServiceModel : BaseModel 

的項目這是我目前:

<DataGrid 
    ItemsSource="{Binding TicketCollection,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" 
    IsReadOnly="True" 
    AutoGenerateColumns="False" 
    DockPanel.Dock="Top"> 

    <DataGrid.Resources> 
     <DataTemplate DataType="{x:Type models:IncidentModel}"> 
      <Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/Active_16.png" /> 

     </DataTemplate> 
     <DataTemplate DataType="{x:Type models:ServiceModel}"> 
      <Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/IncidentMgmt_AllIncidents_16.png" /> 

     </DataTemplate> 
    </DataGrid.Resources> 

    <DataGrid.Columns> 

     <DataGridTemplateColumn Header="Typ"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 

       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 

     <DataGridTextColumn Header="Id" Binding="{Binding Id,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" /> 
     <DataGridTextColumn Header="Titel" Binding="{Binding Title,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" /> 
     <DataGridTextColumn Header="Status" Binding="{Binding Status,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" /> 
     <DataGridTextColumn Header="Erstellung" Binding="{Binding CreatedDate,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" /> 
    </DataGrid.Columns> 
</DataGrid> 

感謝一切可能的幫助!

回答

1

你可以使用一個DataTemplateSelector

public class MyTemplateSelector : DataTemplateSelector 
{ 
    public DataTemplate IncidentTemplate { get; set; } 
    public DataTemplate ServiceTemplate { get; set; } 

    public override DataTemplate SelectTemplate 
     (object item, DependencyObject container) 
    { 
     if (item is IncidentModel) return IncidentTemplate; 
     else if (item is ServiceModel) return ServiceTemplate; 
     else return base.SelectTemplate(item, container); 
    } 
} 

XAML

<DataGrid 

    ... 

    > 
    <DataGrid.Resources> 
     <DataTemplate x:Key="IncidentTemplate" DataType="{x:Type models:IncidentModel}"> 
      <Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/Active_16.png" /> 
     </DataTemplate> 
     <DataTemplate x:Key="ServiceTemplate" DataType="{x:Type models:ServiceModel}"> 
      <Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/IncidentMgmt_AllIncidents_16.png" /> 
     </DataTemplate> 

     <local:MyTemplateSelector x:Key="MyTemplateSelector" 
      IncidentTemplate="{StaticResource IncidentTemplate}" 
      ServiceTemplate="{StaticResource ServiceTemplate}" /> 

    </DataGrid.Resources> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Typ" 
      CellTemplateSelector="{StaticResource MyTemplateSelector}" /> 

     ... 

    </DataGrid.Columns> 
</DataGrid> 
+0

謝謝!奇蹟般有效! – Daniel

1

做這一切的XAML中有一個附加ContentPresenter似乎工作:

<DataGrid> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Typ"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ContentPresenter Content="{Binding}"> 
         <ContentPresenter.Resources> 
          <DataTemplate DataType="{x:Type models:IncidentModel}"> 
           <Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/Active_16.png" /> 
          </DataTemplate> 
          <DataTemplate DataType="{x:Type models:ServiceModel}"> 
           <Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/IncidentMgmt_AllIncidents_16.png" /> 
          </DataTemplate> 
         </ContentPresenter.Resources> 
        </ContentPresenter> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid>