views:

31

answers:

3

I have a TextBox with a height=15 and width=50. I want the textbox to grow when the text size exceeds 50. I want to achieve this without using Width="Auto". Is there any way to achieve this? I tried TextWrapping = TextWrapping.Wrap without any success.

Appreciate your help!!

+2  A: 

Set the MinWidth=50

DrakeVN
A: 

Thanks, Drake. I tried using your mechanism but have an issue. Let me explain the issue. I have this Textbox in an UserControl and using it in other control by making 5 instances of this control. If I set MinWidth=50, the TextBox width occupies the maximum for all instances even though the text size is smaller than 50.

My textbox is defined like this.

        <TextBox  Grid.Column="0" Grid.Row="2" Style="{StaticResource RegTextStyle}" Height="15" MinWidth="50" Text="{Binding Path=RcpConditionSelection, ElementName=ucRcpCondition, Converter={StaticResource EnumToDescriptionConveter}}"                           
                  IsReadOnly="True">
        </TextBox>

My user control is defined like this (width = auto, height auto)

The problem is the textbox size is too long for some controls although the size is 3 characters. How to fix this issue?

Vin
A: 

Let's say your text box is inside a grid which has 2 columns

<Grid>
<Grid.ColumnDefinitions>

   //The first column is used for a label
  <ColumnDefinition Width="Auto"/>

//This column is used for your text box
 <ColumnDefinition Width="*"
                   MinWidth="25"/>
</Grid.ColumnDefinitions>

  <Label Grid.Column="0"
         Content="Something:"
  />
  <TextBox Grid.Column="1"
          Content="BindToProperty"
    />
</Grid>

and the height and width of your user control is set to

Auto

So whenever you place the user control on to other controls, it will have the minimum width of

25 + label width

. If you want to increase the width, you can set the width directly to your user control, and the Textbox will be stretched out.

Cheers

DrakeVN