title: Dirty Rects grouping
body: <p>I have a pretty simple scrollable calendar UI:</p>
  
  <p><img src="https://i.stack.imgur.com/NW4IA.jpg" alt="enter image description here"></p>
  
  <p>But from time to time calendar blinks during scrolling. I have looked at WPF Performance Suite and noticed there is a significant amount of Dirty Rects (about 400):</p>
  
  <p><img src="https://i.stack.imgur.com/2t8xT.jpg" alt="enter image description here"></p>
  
  <p>The markup of the calendar is ItemsControl which binds Days (only visible days are bound). Looks like WPF redraws day by day (so that's why there are so many dirty rects for such a simple UI). I thought may be there is a way to tell WPF not to redraw many small rectangles but redraw the whole ItemsControl at once (similar to what Double Buffering did in the all good days of WinForms).</p>
  
  <p>P. S. WritableBitmap fixes the problem but I hope there is a nicer way</p>
  
  <p><strong>Update.</strong> Here is how Calendar looks if I switch the "Show dirty-region update overlay" option on:</p>
  
  <p><img src="https://i.stack.imgur.com/Kmsld.jpg" alt="enter image description here"></p>
  
  <p>So WPF correctly finds the dirty region. The question is why it decides to use so many dirty-rects to redraw it. My guess is that it happens because of the space between days (one or two pixels of white) which is the same during scrolling.</p>
  
  <p><strong>Update 2.</strong></p>
  
  <p>Here is the markup of the Calender:</p>
  
  <pre><code>&lt;ItemsControl  Panel.ZIndex="1" Grid.Column="1" 
         ItemsSource="{Binding Days}" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Stretch"&gt;
      &lt;ItemsControl.ItemsPanel&gt;
          &lt;ItemsPanelTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"/&gt;
          &lt;/ItemsPanelTemplate&gt;
      &lt;/ItemsControl.ItemsPanel&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;Border Margin="1,0,1,0" Padding="0,0,3,0" 
                    CornerRadius="1" Width="28" Height="28" 
                    VerticalAlignment="Top"&gt;
                  &lt;Border.Background&gt;
                      &lt;MultiBinding Converter="{StaticResource DayOfWeekToColorConverter}"&gt;
                          &lt;Binding Path="IsWeekend"/&gt;
                      &lt;/MultiBinding&gt;
                  &lt;/Border.Background&gt;
                  &lt;StackPanel&gt;
                      &lt;TextBlock  Style="{StaticResource TextStyle}" 
                            HorizontalAlignment="Center" 
                            VerticalAlignment="Center"/&gt;
                      &lt;Label  Style="{StaticResource LabelStyle}" 
                            Content="{Binding Date.Day}" 
                            HorizontalAlignment="Center" 
                            VerticalAlignment="Center"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>