2013-01-25 123 views
0

我有一个包含Grid,从延长UserControl这样一个抽象类继承的多个实例的应用程序:对象的ActualWidth和ActualHeight报告为0?

<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"> 
.... 

,他们从(这是只有几个方法非常基本的类继承的类属性):

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

这些网格是在运行时动态构建的,每个网格可以在大小上有所不同。当用户关闭应用程序时,我想捕获每个Grid的屏幕截图并将它们保存到本地机器。为此,我创建了一个获取对象大小的方法,并使用RenderTargetBitmap()将其保存到文件中。他们中的很多人保存没有问题,但是一些网格返回ActualWidthActualHeight0,因此不保存。我不确定从哪里开始调试这个问题,因为它们都是同一个确切对象的实例;他们中的一些人有他们的ActualWidthActualHeight集,其他人不可以呢?它们在应用程序中都没有问题地显示。

让我知道你是否需要更多信息/代码。谢谢!

+0

你尝试加载用户控件后读取这些值? – iltzortz

+0

也检查在这个链接的答案http://stackoverflow.com/questions/1695101/why-are-actualwidth-and-actualheight-0-0-in-this-case – iltzortz

+0

不,我不这么认为 – Saggio

回答

2

为了完整起见,我能够通过捕捉它们的快照前将这些调用每个UserControl解决问题:

scoreGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 
       scoreGrid.Arrange(new Rect(0, 0, scoreGrid.ScoreGridInstance.DesiredSize.Width, scoreGrid.ScoreGridInstance.DesiredSize.Height));