public class OperateSelector : DataTemplateSelector
{
public DataTemplate ATemplate { get; set; }
public DataTemplate BTemplate { get; set; }
public DataTemplate CTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if(item != null)
{
var status = (DataStatus)item.DataStatus;
if (status == StatusA)
{
return ATemplate;
}
else if (status == StatusB)
{
return BTemplate;
}
else
{
return CTemplate;
}
}
return ATemplate;
}
}
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="ATemplate">
<!-- todo -->
</DataTemplate>
<DataTemplate x:Key="BTemplate">
<!-- todo -->
</DataTemplate>
<DataTemplate x:Key="CTemplate">
<!-- todo -->
</DataTemplate>
<selector:OperateSelector x:Key="OperateSelector"
ATemplate="{StaticResource ATemplate}"
BTemplate="{StaticResource BTemplate}"
CTemplate="{StaticResource CTemplate}" />
</ResourceDictionary>
</UserControl.Resources>
</UserControl>
<Grid>
<DataGrid>
<DataGrid.Columns>
<!-- todo -->
<DataGridTemplateColumn Width="1.5*"
CellTemplateSelector="{StaticResource OperateSelector}"
Header="操作" />
</DataGrid.Columns>
</DataGrid>
</Grid>