WPF DataGrid:按ItemsSoure的DataType选择CellTemplate

时间:2017-04-06 07:15:12

标签: wpf types datagrid celltemplate

我正在构建一个DataGrid,我希望根据当前项的基础数据类型在单元格内切换图像。

问题: 是否可以应用此类型的模板切换? 优选仅在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>

感谢您提供一切可能的帮助!

2 个答案:

答案 0 :(得分:2)

您可以使用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>

答案 1 :(得分:1)

使用额外的ContentPresenter在XAML中完成所有操作似乎有效:

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