Question

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!

Was it helpful?

Solution

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top