【问题标题】:How to prevent growing of TextBox in WPF auto-sized window如何防止文本框在 WPF 自动调整大小的窗口中增长
【发布时间】:2020-07-06 11:08:09
【问题描述】:

我有这个 WPF 应用程序,它显示一个标签、一个按钮和一个文本框,如下所示:

通过按钮再添加三行后,windwos 开始随着文本框增长:

这是 XAML:

<Window x:Class="_99_TestWPFApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Simple WPF Test App" Height="Auto" Width="Auto" SizeToContent="WidthAndHeight">
<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="A LABEL" "/>
    <Button Grid.Row="1" Grid.Column="0" x:Name="btnAdd" Click="btnAdd_Click" Content="ADD LINE" />
    <TextBox  Grid.Row="0" Grid.Column="1"  Grid.RowSpan="2"  VerticalScrollBarVisibility="Visible" Width="150"/>
</Grid>

为了将文本框的高度绑定到网格的高度,我也将它添加到了文本框Height="{Binding ElementName=MainGrid, Path=ActualHeight}",但没有效果。

我希望在文本框通过滚动条显示其内容时固定窗口和网格大小

这甚至可能吗?还是我必须在某处设置一个固定的高度(例如窗口)?

【问题讨论】:

  • 你要解决哪个问题?您有窗口的自动宽度和高度以及SizeToContent 和网格中的相同比例。您的 xaml 根据声明正常工作
  • 设置MaxHeight&lt;TextBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" VerticalScrollBarVisibility="Visible" Width="150" MaxHeight="100"/&gt;
  • 如果可能,我不想在 XAML 中设置任何固定大小。想象一下,我稍后在网格的第一列中添加了一些控件:然后我必须相应地调整文本框的固定大小。
  • stackoverflow.com/questions/4465646/… 也许对你有帮助。
  • @NEBEZ :这只会环绕水平溢出,但不会阻止垂直增长

标签: wpf wpf-controls


【解决方案1】:

其实是可以的:经过一些实验,我找到了一种可能的解决方案:

<Window x:Class="_99_TestWPFApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Simple WPF Test App" Height="Auto" Width="Auto" SizeToContent="WidthAndHeight">
<Grid x:Name="OuterGrid">
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid x:Name="LeftInnerGrid">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="A LABEL"/>
        <Button Grid.Row="1" Grid.Column="0" x:Name="btnAdd" />
    </Grid>
    <!-- notice the additional grid and the binding of the height here -->
    <Grid Name="RightInnerGrid" Grid.Row="0" Grid.Column="1" Height="{Binding ElementName=OuterGrid, Path=ActualHeight}">
        <TextBox   VerticalScrollBarVisibility="Visible" Width="150"/>
    </Grid>
</Grid>

将文本框添加到自己的网格中并将内部网格的高度属性绑定到外部网格的高度就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多