【问题标题】:Text box growing with data in WPF文本框随着 WPF 中的数据增长
【发布时间】:2013-02-09 12:52:49
【问题描述】:

从过去的 3-4 天开始,我正在尝试学习 WPF。 作为示例 xaml,我尝试创建一个网格并以文本块和文本框的形式在其中放置一些控件。

我面临的问题是,当我继续在文本框中输入文本时,我的文本框宽度不断增加,这反过来又会破坏我的整个表单。

谁能帮我解决这个问题。

这是我的示例 XAML 代码,其中包含文本框:

 <Grid>
<Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="2*"/>


        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
  <TextBox  Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"  FontSize="25"  Margin="5 30 130 0"/>
        <TextBox  Grid.Row="2" FontSize="25" Grid.Column="1" Grid.ColumnSpan="2" Margin="5 30 130 0"/>
        <TextBox  Grid.Row="3" FontSize="25" Grid.Column="1" Margin="5 30 160 0"/>
        <TextBox  Grid.Row="5" FontSize="25" Grid.Column="1" Grid.ColumnSpan="2" Margin="5 30 130 0"/>
</Grid>

请任何人在这方面帮助我。我们将不胜感激。

【问题讨论】:

    标签: wpf textbox wpf-controls


    【解决方案1】:

    您可以分别在TextBox 上使用AcceptsReturnTextWrapping 属性来允许换行并允许文本在文本大小超过TextBox 的宽度时换行

    例如

    <TextBox TextWrapping="Wrap" AcceptsReturn="true" /> 
    

    AcceptsReturn 确保用户可以按回车而不是 CTRL + Return 在文本框中放置换行符。否则,文本框将失去焦点,并按照标准行为按下默认按钮。 (我认为如果没有默认按钮,无论如何都会插入换行符)

    【讨论】:

      【解决方案2】:

      您确定 TextBox 变大了还是只是填满了文本?
      为了测试,在其上放置一个边框画笔和 Horizo​​ntalContentAlignment="Stretch"。
      如果要限制宽度,请设置宽度,否则它将使用所有可用空间。

      <Grid>
          <Grid.RowDefinitions>
              <RowDefinition Height="40"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <TextBox Text="text" BorderBrush="Red" BorderThickness="2" HorizontalContentAlignment="Stretch" />
      </Grid>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        相关资源
        最近更新 更多