WPF:如何使文本框动态调整大小但阻止自动调整大小?

flo*_*ele 7 .net wpf layout xaml

我知道在WPF中自动调整文本框的大小有很多问题,但我找不到解决以下问题的方法.

考虑这个简单的窗口:

<Window x:Class="TestVisualBrush.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="470" Width="608">
<ScrollViewer>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBox>Test</TextBox>
        <TextBox MinHeight="100" Grid.Row="1" AcceptsReturn="True" >Test</TextBox>
    </Grid>
</ScrollViewer>
</Window>
Run Code Online (Sandbox Code Playgroud)

这实现了我需要的这两个约束:

  1. 此设置将使第二个文本框动态调整大小,以便使用剩余的窗口空间.
  2. 如果窗口对于所需的最小内容大小而言太小ScrollViewer,则ScrollViewer显示滚动条.

但是,当您在第二个文本框中键入太多文本时,会ScrollViewer显示滚动条而不是TextBox.我想阻止文本框增加其高度超出父级Grid最初给出的空间.我不能MaxHeight在这种情况下使用,因为没有适合ActualHeight绑定(据我所见).

有关如何解决此问题的任何建议(最好没有代码隐藏)?

请注意,如果根ScrollViewer的内容对于窗口来说太大,它仍应滚动.

在HTML中我想要的将转换为:

<table height="100%">
 <tr>
    <td><input type="text"></td>
 </tr>
 <tr height="100%"> 
    <td>
          <!-- Uses as much space as it gets, but scrolls if text inside
               gets too large. Makes outer window scroll if too small
               for min-height and other controls in table. -->
      <textarea style="height:100%;min-height:100px"></textarea>
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

Ash*_*shi 6

可滚动扩展控制问题.

Scrollable-expandable-controls:控件可以随着内容的增长而伸展,并在其大小受限时显示滚动条.

当它们位于另一个可滚动控件内时出现问题.子可滚动扩展控件将继续扩展,并将依赖外部可滚动控件的滚动条.

如果你给它一个最大宽度或高度问题将被解决,但你需要知道前面的大小,如果你想要一个适用于所有不同屏幕尺寸的动态应用程序,你就没有这个特权.

为了实现所需的行为,我们需要在它们之间使用一个面板来允许它的子节点(可滚动扩展控件)增长,要求它们给出所需的最小尺寸,然后给它们父节点提供的最大尺寸而不显示滚动条,目前在那里不是这样的小组.

这是我开发的用于提供此功能的一个:

    class LimitChild : System.Windows.Controls.Panel
    {
        public LimitChild()
        {
        }

        protected override Size MeasureOverride(System.Windows.Size availableSize)
        {
            System.Diagnostics.Debug.Assert(InternalChildren.Count == 1);
            System.Windows.UIElement child = InternalChildren[0];

            Size panelDesiredSize = new Size();
            // panelDesiredSize.Width = availableSize.Width;
            panelDesiredSize.Width = (double)child.GetValue(FrameworkElement.MinWidthProperty);
            panelDesiredSize.Height = (double)child.GetValue(FrameworkElement.MinHeightProperty);

            child.Measure(panelDesiredSize);

            // IMPORTANT: do not allow PositiveInfinity to be returned, that will raise an exception in the caller! 
            // PositiveInfinity might be an availableSize input; this means that the parent does not care about sizing 
            return panelDesiredSize;
        }

        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
        {
            System.Windows.UIElement child = InternalChildren[0];

            child.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
            if (finalSize.Width > child.RenderSize.Width)
                finalSize.Width = child.RenderSize.Width;
            if (finalSize.Height > child.RenderSize.Height)
                finalSize.Height = child.RenderSize.Height;

            return finalSize; // Returns the final Arranged size
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后在你的xaml里面填充你的可滚动扩展控件.

        <l:LimitChild
            Grid.Row="1">
            <TextBox
                VerticalScrollBarVisibility="Auto"
                HorizontalScrollBarVisibility="Auto"
                MinHeight="200"
                AcceptsReturn="True">Test</TextBox>
        </l:LimitChild>
Run Code Online (Sandbox Code Playgroud)