tags:

views:

39

answers:

2

i would like to get a grids 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. any ideas? thx ;)

+2  A: 
<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.

Matt Hamilton
and actually, the `Actual*` properties reside quite high up in the hierarchy and are therefore available on many other controls besides the grid.
David Schmitt
nice :D thx... any idea how to implent this "only" in codebehind? for example if i create a grid mygrid = new grid() and the tb TextBox mytb = new TextBox() how to assign it there? .. mytb.Text=mygrid.actualwidth; ? or something with property changed or like this?
mike
Same thing in code:mytb.SetBinding(TextProperty, new Binding("ActualWidth") { Source=mygrid });
John Bowen
@John I think that'd be { ElementName = "myGrid" }, wouldn't it? Or would Source work too?
Matt Hamilton
@Matt - No, that would only work if you defined an element named "myGrid" in XAML in the same scope where you're adding the TextBox. Variable names don't carry over from code into XAML. ElementName is actually just a shortcut for setting the Source that's used to get around the fact that you can't bind an element to a property of a binding in XAML (this was a problem in Silverlight pre-3.0). They have the same effect but use different types of references.
John Bowen
*slaps forehead* :)
Matt Hamilton
A: 

ok thx :D works all fine... but now i would 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, pls help :)

mike