阻止 WPF TextBox 随文本一起增长 [英] Stopping WPF TextBox from growing with text

查看:24
本文介绍了阻止 WPF TextBox 随文本一起增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的控件的xaml:

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>

有什么想法吗?我需要 zoom.png 出现在文本框的内部",所以我使用了一个水平堆栈面板,并将图像和文本框并排放置,两者都被相同的边框包围.

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?

谢谢.

更新:

下面是我正在测试的 xaml.

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>

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

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

推荐答案

我进行了更多搜索并找到了解决方案.我使用下面的 Grid 替换了我原始帖子中的网格,现在文本框保持其原始大小,并且不会在长时间的用户输入时自动增长.感谢所有调查此事的人.

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>

这篇关于阻止 WPF TextBox 随文本一起增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆