【问题标题】:Stopping WPF TextBox from growing with text阻止 WPF TextBox 随文本一起增长
【发布时间】:2017-01-18 22:43:06
【问题描述】:

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

这是我的控件的 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 出现在文本框的“内部”,所以我使用水平堆栈面板,只需将图像和文本框并排放置,都被相同的边框包围。

有没有办法让我在输入文本时阻止我的文本框自动增长?

谢谢。

更新:

下面是我正在测试的 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>

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

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

    我进行了更多搜索并找到了解决方案。我使用下面的 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>
    

    【讨论】:

    • 谢谢。我试过这个,它对我来说有一个缺陷。如果您使用滚动查看器,则无法使用它。我最初有一个,发生的情况是当 UI 增长时文本框会增长,但当 UI 缩小时不会缩小。
    • @nameless 您必须为您的 Scrollviewer 设置 HorizontalScrollBarVisibility = "False" 以使您的文本框在缩小时跟随窗口宽度。
    • 如果这是唯一真正的解决方案,这应该在 WPF 本身中得到修复......(我自己还没有找到更好的东西,我们在 2016 年......)
    • 实际上,我回来了。如果为 Grid 或 ListView 自己的 ScrollViewer 将 Horizo​​ntalScrollBarVisibility 设置为禁用,则不需要使用 Border 技巧。在我这样做之后,一切都按照我的预期和希望进行。
    【解决方案2】:

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

    【讨论】:

    • 这是最好的答案。
    【解决方案3】:

    将 Grid 命名为 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> 
    

    【讨论】:

    • 嘿基肖尔。我将文本框宽度绑定到网格的宽度,并且文本框在我键入时不会增长,但是现在当我将用户控件放在窗口中时(例如将其添加到窗口中的换行面板),它总是占据整个窗口的宽度。有没有办法将其限制为包含两个文本块的换行面板的宽度?
    • 我已经更新了我的原始帖子以包含我正在测试的 xaml。
    【解决方案4】:

    要停止增长行为,请为 TextBox 设置显式大小,或将其放置在将 Horizo​​ntalAlignment 设置为拉伸的网格中

    选项 1:

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

    选项 2:

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

    【讨论】:

    • 你能发布 xaml 的样子吗?我尝试将文本框包装在网格中并将文本框的 Horizo​​ntalAlignment 设置为 Stretch 但这并没有阻止文本框随着文本的输入而增长。 (我还尝试将 Grid 的 Horizo​​ntalAlignment 设置为 Stretch,以防我误解,但结果是一样的。)
    【解决方案5】:

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

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

    【讨论】:

    • 谢谢。我一直在寻找一种不设置显式尺寸的方法,但如果找不到解决方案,我可能需要这样做。
    • @Flack:您是否希望它扩展以填充您控件中的某些特定区域?
    • @PhilB:现在我认为文本框的大小将始终保持不变,但将来可能会改变。我只是想避免硬编码大小,因为这似乎是一个在 WPF 中应该尽可能避免的 winforms 事情。所以基本上,我希望带有图像和文本框的 StackPanel 与包含两个文本块的 WrapPanel 一样宽。
    • @Flack:好的,我更新了我的答案以反映这一点。我认为这应该可行。
    • 嘿PhilB,它似乎不起作用。进行您建议的更改时,文本框仍然会随着我键入而增长。
    【解决方案6】:

    本文展示了在滚动查看器的视口(在树视图或列表框中)显示的文本框的解决方案。使用 PART_MeasureTextBlock(绑定到文本框)来确定可用大小并将其设置为文本框。 请看这里并告诉我您对此的看法: http://www.codeproject.com/Articles/802385/A-WPF-MVVM-In-Place-Edit-TextBox-Control

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多