What is the difference between Width and ActualWidth in WPF?

前端 未结 7 750
不知归路
不知归路 2020-11-27 05:23

I am currently working with Panels in WPF, and I noticed that as regards the Width and Height properties, there are also two other pro

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:16

    There is a very good reason not to use the ActualWidth to bind to (obviously ActualHeight accordingly). When you set the Width of an element, to the ActualWidth of another one you may break the layout chain.

    In the best case your element/control needs to be parsed after the layout process of the parent (the binding source) finished. That means additional time. If it is at the same hierarchy level as the parent the layout process needs two runs (at least) to calculate a definitive size.

    For example I had a control which had it's size property overridden in a style that would set it to the TemplatedParent (don't do):

    
    

    When resizing the containing window, the control would prevent the container from becoming smaller and brake the layout. Setting it to the Width will resolve the problem (do):

    
    

    If you have to use the ActualWidth in general something is wrong with your xaml. Better fix that instead of messing up with the final sizes of the layout run.

提交回复
热议问题