views:

61

answers:

4

I am trying to modify my simple control so that the text box does not grow as the user types in long text. I took a look at some solutions posted here at Stackoverflow that suggest using a Grid and an invisible Border and binding the Width of the text box to the ActualWidth of the Border, but I can't seem to get it to work in my setup.

Here is the xaml of my control:

<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
  <TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
  <TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
        BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
  <StackPanel Orientation="Horizontal">
    <Image Source="..\Resources\zoom.png" Width="13"/>
    <TextBox Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
  </StackPanel>
</Border>
</StackPanel>

Any ideas? I need the zoom.png to appear "inside" of the text box so I use a horizontal stack panel and just place the image and text box side by side, both surrounded by the same border.

Is there a way for me to stop my text box from auto growing as text is typed?

Thanks.

UPDATE:

Below is the xaml I am testing with.

<Window x:Class="Desktop.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:composite="http://www.codeplex.com/CompositeWPF"
    Title="MyShell" Height="50" Width="900"
    WindowStyle="None"
    ShowInTaskbar="False"        
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="CanResizeWithGrip"
    WindowStartupLocation="CenterScreen">
  <Border BorderBrush="Black"
                BorderThickness="1.5"
                CornerRadius="5"
                Background="Gray">
    <ItemsControl composite:RegionManager.RegionName="MainRegion">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <WrapPanel></WrapPanel>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
    </ItemsControl>
  </Border>
</Window>


<UserControl x:Class="Desktop.SearchTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="50" Margin="0">
  <StackPanel Margin="5,0">
    <WrapPanel Margin="0,0,0,5">
      <TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
      <TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
    </WrapPanel>
    <Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
        BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
        <Grid x:Name="grdTest">
          <Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
          <TextBox Margin="16,0,0,0"  Foreground="White" Background="Black" BorderBrush="Transparent" Width="{Binding ElementName=grdTest, Path=ActualWidth}">THIS IS SOME TEXT</TextBox>
        </Grid>
    </Border>
  </StackPanel>
</UserControl>

I just add my user control to the Window's MainRegion.

A: 

To scale the border containing the textbox to the width of the outter stack panel, change the inner stack panel to a grid and set a left margin on the text box so it doesn't overlap with the image. Also set the image's horizontal alignment to left (if that is where you want it) or it will default to the center of the border.

        <StackPanel Margin="5,0">
        <WrapPanel Margin="0,0,0,5">
            <TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
            <TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
        </WrapPanel>
        <Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
                BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
            <Grid>
                <Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
                <TextBox Margin="16,0,0,0"  Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
            </Grid>
        </Border>
    </StackPanel>
PhilB
Thanks. I was looking for a way to do it without setting explicit sizes, but I might need to do it this way if I can't find a solution.
Flack
@Flack: Did you want it to scale to fill out some specific area in your control?
PhilB
@PhilB: Right now I think the text box will always be a constant size, but that may change in the future. I just wanted to avoid hard coding sizes as that seems like a winforms thing that should be avoided if possible in WPF. So basically, I want my StackPanel with the image and text box to be as wide as my WrapPanel that contains the two text blocks.
Flack
@Flack: Alright, I updated my answer to reflect that. I think this should work.
PhilB
Hey PhilB, it doesn't seem to work. Making the changes you suggested the text box still grows as I type.
Flack
Seems to work on my machine. The only other thing I could think of right now would be to set HorizontalAlignment="Stretch" on the TextBox element.
PhilB
A: 

Too stop the grow behavior give it a explicit size or place it in a grid with the HorizontalAlignment set to strecth

rudigrobler
Can you post what the xaml would look like? I tried wrapping the text box in a grid and setting the text box's HorizontalAlignment to Stretch but that didn't prevent the text box from growing as text was typed. (I also tried setting the Grid's HorizontalAlignment to Stretch in case I misunderstood but the result was the same.)
Flack
A: 

Name the Grid as x:Name=grdTest and try this

<Grid x:Name=grdTest> 
            <Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/> 
            <TextBox Margin="16,0,0,0"  Foreground="White" Background="Black" BorderBrush="Transparent" Width="{Binding ElementName=grdTest, Path=ActualWidth}">THIS IS SOME TEXT</TextBox> 
        </Grid> 
Kishore Kumar
Hey Kishore. I bound the text box width to the grid's width and the text box does not grow as I type but now when I put the user control in a window (such as adding it to a wrap panel in a window) it always takes up the entire width of the window. Is there a way to have it limited to the width of the wrap panel that contains the two text blocks?
Flack
can you share the xaml?
Kishore Kumar
I have updated my original post to include the xaml I am testing with.
Flack
A: 

I did some more searching and found the solution. I used the below Grid to replace the grid in my original post and now the text box keeps its original size and does not auto grow on long user input. Thanks to all who looked into this.

      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Image Source="..\Resources\zoom.png" Width="13"/>
        <Border x:Name="b" Grid.Column="1"/>
        <TextBox Width="{Binding ActualWidth, ElementName=b}" Foreground="White" Background="Black" BorderBrush="Transparent"
          Grid.Column="1"
          VerticalAlignment="Center"
          Text="THIS IS SOME TEXT"/>
      </Grid>
Flack