簡體   English   中英

對象的ActualWidth和ActualHeight報告為0?

[英]Object's ActualWidth and ActualHeight reporting as 0?

我有一個應用程序,其中包含一個Grid幾個實例,它繼承自一個擴展UserControl的Abstract類,如下所示:

<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而其他人沒有? 它們都在應用程序中顯示沒有問題。

如果您需要更多信息/代碼,請與我們聯系。 謝謝!

為了完整起見,我能夠通過在捕獲每個UserControl的屏幕截圖之前添加這些調用來解決問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM