2010-09-15 26 views
18

我想修改我的简单控件,以便文本框不会随着用户键入长文本而增长。我看了一下在Stackoverflow发布的一些解决方案,这些解决方案建议使用网格和不可见的边框,并将文本框的宽度绑定到边框的ActualWidth,但我似乎无法让它在我的设置中工作。停止WPF文本框与文本增长

这是我控制的XAML:

<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> 

任何想法?我需要zoom.png出现在文本框的“内部”,所以我使用水平堆叠面板,并将图像和文本框并排放置,两者都被相同的边框包围。

有没有一种方法可以让我停止自动增长文本框的文本输入?

谢谢。

UPDATE:

下面是我与测试XAML。

<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> 

我只是将我的用户控件添加到窗口的MainRegion。

回答

14

我做了一些更多的搜索,找到了解决办法。我使用下面的Grid替换原始文章中的网格,现在文本框保持其原始大小,并且不会在长用户输入时自动增长。感谢所有看过这个的人。

 <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> 
+0

谢谢。我试过这个,它对我有一个缺陷。如果您使用滚动查看器,则无法使用它。我原来有一个,发生的事情是,当用户界面增长时,文本框增长,但当用户界面缩小时不会缩小。 – famousKaneis 2015-06-01 17:08:28

+1

@nameless你必须为你的滚动查看器设置'Horizo​​ntalScrollBarVisibility =“False”'让你的文本框在缩小时跟随窗口宽度。 – M463 2015-07-30 14:03:06

+0

如果这是唯一真正的解决方案,这应该在WPF本身中得到修复......(我自己还没找到更好的东西,而且我们在2016年......) – Arwin 2016-03-16 19:13:19

0

要将包含文本框的边框缩放到outter堆栈面板的宽度,请将内部堆栈面板更改为网格,并在文本框上设置左边距,使其不与图像重叠。还要将图像的水平对齐设置为左侧(如果这是您想要的位置),或者它将默认为边框的中心。

 <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> 
+0

谢谢。我正在寻找一种不设置明确尺寸的方法,但如果我找不到解决方案,我可能需要这样做。 – Flack 2010-09-15 20:41:32

+0

@Flack:你是否想要扩展以在控件中填充某个特定区域? – PhilB 2010-09-15 20:58:08

+0

@PhilB:现在我认为文本框的大小始终是一个常数,但在将来可能会改变。我只是想避免硬编码的大小,因为这似乎是一个winform的东西,如果可能的话应该避免在WPF中。所以基本上,我希望我的StackPanel的图像和文本框的宽度与包含两个文本块的WrapPanel一样宽。 – Flack 2010-09-15 21:49:26

0

要停止成长的行为明确的大小设置为文本框或设置舒展的Horizo​​ntalAlignment其放置在一个网格

选项1:

<TextBox Width="60">THIS IS SOME TEXT</TextBox> 

选项2 :

<TextBox HorizontalAlignment="Stretch">THIS IS SOME TEXT</TextBox> 
+1

你可以发布xaml的样子吗?我尝试在文本框中包装文本框,并将文本框的Horizo​​ntalAlignment设置为Stretch,但这并不妨碍文本框随文本输入而增长。 (我也尝试将Grid的Horizo​​ntalAlignment设置为Stretch,以防我误解,但结果相同。) – Flack 2010-09-15 20:40:43

5

名称网格为x:Name="grdTest"和尝试这个

<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> 
+0

Hey Kishore。我将文本框宽度绑定到网格的宽度,文本框不会像我输入时那样增长,但现在,当我将用户控件放入窗口中时(例如将其添加到窗口中的包装面板),它总是占用整个窗户的宽度。有没有办法限制包含两个文本块的包装面板的宽度? – Flack 2010-09-16 18:56:34

+0

你能分享xaml吗? – 2010-09-17 04:58:44

+0

我已更新我的原始文章,以包括我正在测试的xaml。 – Flack 2010-09-17 12:57:29

5

尝试将ScrollViewer.HorizontalScrollBarVisibility="Disabled"放入Grid中。

+0

这是最好的答案。 – Arwin 2016-03-17 22:38:23