2012-04-25 83 views
1

在我的項目中有很多嵌套的網格。大多數行和列的寬度在XAML中定義爲「*」。

我想通過將其他行的高度設置爲0來擴展特定行(讓我們說Row1),並且我使用.ActualHeight屬性來獲取Row1的寬度,然後它不會給我實際的高度。

據我所知,這是因爲網格行和列的高度和寬度都設置爲渲染時間。
我在網上搜索和有人建議使用UpdateLayout()方法..但那也不適合我。
我無法發佈代碼片斷,因爲它代碼很長。
我在c#.net wpf中的項目。如何在渲染前獲取網格行的實際高度

回答

4

你需要做一個完整的佈局更新,那就是你需要調用措施,安排和UpdateLayout請:

//Make the framework (re)calculate the size of the element 
    _Element.Measure(new Size(Double.MaxValue, Double.MaxValue)); 
    Size visualSize = _Element.DesiredSize; 
    _Element.Arrange(new Rect(new Point(0, 0), visualSize)); 
    _Element.UpdateLayout(); 

_Element是一個FrameworkElement的(電網是一個)。

+0

工作就像一個魅力,謝謝你先生 – JohnChris 2017-05-12 08:06:40

0

基於Baboon post的解決方案爲我工作。我需要對摺疊的FrameworkElement中的幻燈片進行動畫處理,不知道ActualHeight值的類似問題。

element.Visibility = Visibility.Visible; 
    element.InvalidateArrange(); 
    element.UpdateLayout(); 

    double elementHeight = element.DesiredSize.Height; 
    DoubleAnimation animation = new DoubleAnimation(0, elementHeight, TimeSpan.FromMilliseconds(animMilisec)); 
    element.BeginAnimation(Rectangle.HeightProperty, animation); 

現在在呈現元素之前,我有權訪問它的最終高度;

相關問題