Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping WPF TextBox from growing with text

Tags:

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.

like image 514
Flack Avatar asked Sep 15 '10 18:09

Flack


2 Answers

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> 
like image 151
Flack Avatar answered Oct 01 '22 04:10

Flack


Try to put ScrollViewer.HorizontalScrollBarVisibility="Disabled" inside Grid.

like image 40
kostas Avatar answered Oct 01 '22 02:10

kostas