Different cell styles in datagrid wpf depending on the data type in ItemsSource

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

I have a wpf datagrid from the wpf toolkit. Separate lines in the grid should be designed depending on the type of item from the ItemsSource collection. Thus, all elements have the same type of base class, but the columns of some derived types must have a different style.

Is it possible?

Thanks: -)

0
source share
2 answers

, . , , "typewitch", . :

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();
    }
}

Style . , ListBox, :

    <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

WPF-:

, - - , WPF-. DataGridColumn, DataTemplate, ContentPresenter :

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

ContentPresenter DataTemplate CellTemplate, .

+4

All Articles