0

I have an application that contains several instances of a Grid that inherits from an Abstract class extending UserControl like this:

<abstract:ScoringGrid x:Class="ReadProject.Repeater.ScoringGrid"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:abstract="clr-namespace:ReadProject.AbstractRepeater"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid x:Name="ScoringGridInstance" Width="Auto" Height="Auto">
....

and the class they inherit from (which is a very basic class with just a few methods and properties):

public abstract partial class ScoringGrid : UserControl, INotifyPropertyChanged
{...

These grids are built dynamically at runtime and each one can vary in size. When the user closes the application, I want to capture a screenshot of each Grid and save them to the local machine. To do this, I created a method to get the objects size and save it to a file using RenderTargetBitmap(). A lot of them save without issue, however some of the grids are returning an ActualWidth and ActualHeight of 0 and thus not saving. I'm uncertain of where to start debugging this issue since they are all instances of the same exact object; how could it be possible that some of them have their ActualWidth and ActualHeight set and others do not? They all display without issue in the application.

Let me know if you need more information/code. Thanks!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Saggio
  • 2,212
  • 6
  • 33
  • 50
  • Do you try to read these values after user control is loaded? – iltzortz Jan 25 '13 at 14:03
  • also check the answer in this link http://stackoverflow.com/questions/1695101/why-are-actualwidth-and-actualheight-0-0-in-this-case – iltzortz Jan 25 '13 at 14:06
  • @iltzortz I looked at the question you linked and tried looping through each `Grid` and called `Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));` on each one before attempting to get the size; it results in the same issue. – Saggio Jan 25 '13 at 14:17
  • i was talking about the part of the answer that says: `You can force these to be computed earlier by simply calling the window's Measure() and Arrange() methods manually after the window's InitializeComponent() returns.` No arguments to the measure and arrange functions – iltzortz Jan 25 '13 at 14:35
  • I saw this in a comment for that answer which is why I passed in the parameters to `Measure()`: `window.Measure returns void.` I did, however, add a call to `Arrange()` for each one and that seemed to work. What exactly are those doing, though, and why are they necessary? Also I have bigger `Grids` that encapsulate two of the `ScoringGrid`s, but when I try to do the same thing with those, only the top portion is captured and not the whole thing, any idea why? – Saggio Jan 25 '13 at 15:59

1 Answers1

2

For completeness, I was able resolve the issue by adding these calls to each UserControl before capturing a screenshot of them:

scoreGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                scoreGrid.Arrange(new Rect(0, 0, scoreGrid.ScoreGridInstance.DesiredSize.Width, scoreGrid.ScoreGridInstance.DesiredSize.Height));
Saggio
  • 2,212
  • 6
  • 33
  • 50