DataGrid rendering fails

Posted by patryk.beza on Stack Overflow See other posts from Stack Overflow or by patryk.beza
Published on 2012-10-07T21:34:58Z Indexed on 2012/10/07 21:37 UTC
Read the original article Hit count: 446

I have DataGrid with groups of data.
The problem is that after binding data I have strange effect (text was blured by me; the problem are cells' paddings/margins).

This effect can be easily 'fixed' by user because after one click on top expander data hides and after second click on the expander, rows in DataGrid are displayed correctly.

My XAML code:

<DataGrid Name="myDataGrid" Grid.Row="0" ItemsSource="{Binding}" AutoGenerateColumns="False" Background="White" RowBackground="#FBFFFA" AlternatingRowBackground="#EEFAEB" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <DataGrid.Columns>
        <!-- Columns definitions with binding ( . . . ) -->
    </DataGrid.Columns>

    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Padding" Value="7,3"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
                                <GradientStop Color="#008C13" Offset="0"/>
                                <GradientStop Color="#19FF38" Offset="0.8"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>

    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>

            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Rok " />
                                            <TextBlock Text="{Binding Name}" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>

        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

DataGrid's DataContext is set from code (rows with data in DataGrid are displayed after clicking proper button):

ICollectionView myView = CollectionViewSource.GetDefaultView(myList);

if (operationsView.GroupDescriptions.Count > 0)
    operationsView.GroupDescriptions.Clear();

operationsView.GroupDescriptions.Add(new PropertyGroupDescription("myGroupDescProperty"));
FinancialIncomeOperationsListDataGrid.DataContext = operationsView;

Is there any way to manually update layout of the DataGrid? Or maybe there is a better solution?

© Stack Overflow or respective owner

Related posts about datagrid

Related posts about datagridview