1

I'm working on custom control which uses VirtualizingStackPanel with mode set to "recycling" and I run into following problem: https://i.stack.imgur.com/76oSZ.png

It happens when I "play" with columns width for a while and then maximize the window. I don't know what causes this. PrepareContainerForItemOverride is called, I checked cells width and it is correct, it seems that StackPanel which holds cells just isn't rearranging its contents and I don't know how to force it to do so...

I know I'm not giving too much detail but maybe some of you had similar problem and just happen to know the solution.

EDIT: XAML for relevant part:

<Style x:Key="{x:Type dgp:CellsPresenter}" TargetType="{x:Type dgp:CellsPresenter}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type dgp:CellsPresenter}">
                <ItemsPresenter/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="{x:Type dg:Cell}" TargetType="{x:Type dg:Cell}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
    <Setter Property="BorderThickness" Value="0,0,1,1" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type dg:Cell}">
                <Border Background="{TemplateBinding Background}" 
              BorderBrush="{TemplateBinding BorderBrush}"  
              BorderThickness="{TemplateBinding BorderThickness}" 
              SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
        </Trigger>
    </Style.Triggers>
</Style>

Each row (simple control, XAML irrelevant) holds CellPresenter and rows are stacked into VirtualizingStackPanel. It seems that StackPanel with horizontal orientation that you can see in ItemsPanelTemplate is not updating its layout.

zduny
  • 2,481
  • 1
  • 27
  • 49
  • If you would post the XAML we could review it. – paparazzo May 13 '12 at 17:11
  • Well, It's quite big and not really that relevant since there is code behind but the part that isn't updated is relatively simple, I updated the question. – zduny May 13 '12 at 17:26
  • Still don't know what is causing the problem but ended up writing a bit dirty, simple hack that seems to work: `private void DataGrid_SizeChanged(object sender, SizeChangedEventArgs e) { foreach (var r in Rows) { r.Width = 0; r.UpdateLayout(); r.Width = double.NaN; } }` – zduny May 13 '12 at 18:43

0 Answers0