views:

298

answers:

2

Hello,

I tried searching on stackoverflow for an answer to my question but couldn't seem to find anything relevant.

Since I'm relatively new to Silverlight, I'm not even sure if this is possible, but I am trying to figure out if it is possible to set a Silverlight textbox control's width value proportionally to it's immediate parent container?

It would be nice to avoid hard coding a width value. Say you want to place a textbox control inside a horizontally aligned StackPanel control and have it's width always dynamically adjusted to be 80% of the total width of the StackPanel.

Is this possible to do declaratively in your xaml markup, or will I need to resort to some codebehind attached to some event handler to accomplish this?

fyi, I am currently using Visual Web Developer Express 2008 to write my silverlight code.

thanks in advance, John

+1  A: 

It sounds like you want a <Grid> instead of a <StackPanel>. Just put the TextBox into a column whose width is set to .8.

Gabe
+1  A: 

Use a Grid instead of a stack panel. Something like this would work.

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="80*" />
        <ColumnDefinition Width="20*" />
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" />
</Grid>

Just set the columndefinition widths to be proportional to your needs. If you want a specific width, leave out the * (asterisk). If you want proportional, add an * (asterisk) and use whatever scheme you want for determining the proportions (they can add up to whatever number you want). In my example, the numbers add to 100. But you can use fractional numbers, or whatever.

WPCoder