首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >停止使用文本增长WPF TextBox

停止使用文本增长WPF TextBox
EN

Stack Overflow用户
提问于 2010-09-16 02:59:17
回答 6查看 20.5K关注 0票数 21

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

下面是我的控件的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>

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-09-20 21:41:50

我做了更多的搜索,找到了解决方案。我使用下面的网格来替换我的原始帖子中的网格,现在文本框保持其原始大小,并且不会在长用户输入时自动增长。感谢所有关注这个问题的人。

      <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>
票数 16
EN

Stack Overflow用户

发布于 2010-12-28 00:41:44

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

票数 10
EN

Stack Overflow用户

发布于 2010-09-16 11:51:33

将网格命名为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> 
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3720855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档