tags:

views:

23

answers:

2

Hi,

I'm having issues working out how to achieve the "anchor left and right" property from WinForms in Silverlight.

Here's the XAML showing my issue:

        <Grid Width="400" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"  />
               <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

             <TextBlock Grid.Column="0" Text="Some label" />
             <TextBox Grid.Column="1" />
         <Grid>

What I expect is to have the label on the left and the TextBox on the right fill up the Grid. On the initial load, that's what happens. However, if I type a very long text in the TextBox, then it resizes itself to fit its content and becomes bigger than the Grid.

Is it possible to prevent the TextBox to resize itself and just make it take as much space as available as when setting anchor left and right in WinForms?

Maybe there's an easy property to set that I completely missed.

EDIT: Apologies, I missed a crucial element in the XAML (the Grid.Column= bit) :(

I DO know the sizing properties of SL (Star, Auto, Pixel). My mistake made it unclear what I wanted to achieve.

In the corrected example, the TextBox will fill the Grid but typing a very long text will make it grow beyond the Grid's width.

Cheers.

A: 

Unless I'm misunderstanding your question all you need to do is get rid of the <ColumnDefinition Width="Auto" />. Your TextBox and TextBlock are in the first column of the Grid which is set to Auto. If you get rid of the first column definition they will be in the star-column which will fill the entire area, but wont allow them to resize to their content.

EDIT

Have you tried setting HorizontalAlignment="Stretch" VerticalAlignment="Stretch" on you TextBox? That should cause the TextBox to use a fixed size and not resize based on content.

Stephan
Sorry, I made a mistake in my XAML so my question wasn't clear. It's corrected now.
R4cOON
Unless I'm mistaken that's the default setting. I do want the TextBox to fill the grid column and that's what happens. But I don't want it to exceed the column's width.I tried hooking up on the SizeChanged event and do something but unfortunately the first sizing is done according to the content bound to it :(
R4cOON
A: 

In Silverlight column and row definitions are either:

  • Pixel - means "make me this exact size"
  • Auto - means "resize to fit the largest child object in that col/row" and
  • Star - means "give me a share of the remainder after Pixel and Auto have taken their share".

The default if none of these is specified is Star.

"Star" uses numbers that are only relative to other star columns/row and have no absolute value. Two columns with 1* and 2* are the same as 1000* and 2000*. Basically they would take 1/3rd and 2/3rds of any space left after any Pixel and Auto columns/rows.

As per the other answer if you want a fixed width that is a percentage of the grid you want a star column. Not an Auto column which will resize to fit the growing text box as you found.

Enough already
It seems to me that as stated setting the size to star DOESN'T set a fixed size but is rather an expanded auto.So typing some long text will resize the box (which I don't want) and binding to a long text will do the same.
R4cOON