码迷,mamicode.com
首页 > 其他好文 > 详细

添加到界面前获取尺寸

时间:2018-08-30 13:18:59      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:copy   set   delegate   explicit   public   over   tac   blog   already   

原文:添加到界面前获取尺寸

You have few options you can do this, forcing to call Window.Measure and Window.Arrange will make all values to be calculated, or you can get those values in the Window.Loaded event. This same issue is discussed already on this question.

If you are sizing to content:

window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
window.Arrange(new Rect(0, 0, window.DesiredWidth, window.DesiredHeight));

If you are using an explicit window size:

window.Measure(new Size(Width, Height));
window.Arrange(new Rect(0, 0, window.DesiredWidth, window.DesiredHeight));

or

public MyWindow()
{
    Loaded += delegate
    {
        // access ActualWidth and ActualHeight here
    };

}



Just had the same issue and just calling textblock.Measure(new Size()); aint enough. I had to usetextblock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); to get a result for textblock.DesiredSize



The key is to realize that setting it in code like this:

sp2.Margin = new System.Windows.Thickness{ Left = 5 };

is equivalent to:

sp2.Margin = new System.Windows.Thickness{ Left = 5, Top = 0, Right = 0, Bottom = 0 };
 

添加到界面前获取尺寸

标签:copy   set   delegate   explicit   public   over   tac   blog   already   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9559547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!