Different cell styling in a wpf datagrid depending on datatype in the ItemsSource

淺唱寂寞╮ 提交于 2020-01-03 06:32:09

问题


I am wondering if it was possible to change the styling of a column in a wpf datagrid depending on the type of item in the ItemsSource collection.

I have a wpf datagrid from the wpf toolkit. The single rows in the grid should be styled depending of the type of item from the ItemsSource collection. So all items are of the same base class type but the columns of some derived types should get a different stylization.

Is this possible?

Thank you :-)


回答1:


Yes, it is possible to do it in several ways. The one I would go for is writing your own custom "typeswitch" converter that selects a value depending on type of input. Like this:

public class TypeSwitchConverter : Dictionary<Type, object>, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, .CultureInfo culture)
    {
        foreach (var mapping in this)
        {
            if (mapping.Key.IsAssignableFrom(value.GetType()))
            {
                return mapping.Value;
            }
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And then use a binding for the Style of the top-level element in template for your cell, and use the above converter for that binding as needed. Here's a simplified example that styles items in a ListBox using it:

    <ListBox ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">
                    <TextBlock.Style>
                        <Binding>
                            <Binding.Converter>
                                <my:TypeSwitchConverter>
                                    <Style x:Key="{x:Type cor:Int32}" TargetType="{x:Type TextBlock}">
                                        <Setter Property="Background" Value="Red" />
                                    </Style>
                                    <Style x:Key="{x:Type cor:String}" TargetType="{x:Type TextBlock}">
                                        <Setter Property="Background" Value="Green" />
                                    </Style>
                                    <Style x:Key="{x:Type sys:Uri}" TargetType="{x:Type TextBlock}">
                                        <Setter Property="Background" Value="Blue" />
                                    </Style>
                                </my:TypeSwitchConverter>
                            </Binding.Converter>
                        </Binding>
                    </TextBlock.Style>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>



回答2:


A WPF-only solution:

I am a little late for the party but if someone else is trying to do something like this, there is a WPF only solution. There are no DataGridColumn class that will directly look for the proper DataTemplate but we can indirectly use a ContentPresenter like this:

<Window.Resources>
    <DataTemplate DataType="{x:Type models:Employee}">
        <Grid>...</Grid>
    </DataTemplate>

    <DataTemplate DataType="{x:Type models:Manager}">
        <Grid>...</Grid>
    </DataTemplate>
</Window.Resources>

<DataGrid ... >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="MyColumn">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ContentPresenter Content="{Binding}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

By sandwiching a ContentPresenter inside a DataTemplate inside the CellTemplate, we achieve the desired result.



来源:https://stackoverflow.com/questions/1198661/different-cell-styling-in-a-wpf-datagrid-depending-on-datatype-in-the-itemssourc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!