开发者

How to get Width of a Grid on Property Changed in WPF

I would like to get a Grid's actual Width (or Height) and display it in a textbox, but the width/height should be "live", when I resize the window I would like to see the new sizes.

How do I do this?

Update:

With regard to Matt's answer:

This works fine but now i w开发者_StackOverflow中文版ould like to go a step further.

I need the actual width in a variable for testing purposes like to set some if statements or to scale other objects that are in a grid.

How do I do this?


<Grid x:Name="myGrid">...</Grid>

<TextBox Text="{Binding ActualWidth,ElementName=myGrid}" />

(The TextBox could, of course, be inside the Grid.)

The trick is that Width only specifies the initial width of a control. ActualWidth is the "live" property you should bind to.


To answer your second set of questions:

You can add a DependencyProperty to the Window and bind it to the ActualHeight of the Grid in the same way the Text property of the TextBox was bound.

It is also possible to bind other properties of other controls to the ActualHeight of the Grid. If you need a calculation inbetween use a ValueConverter to change the value to something you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜