WPF - 如何阻止 TextBox 自动调整大小?

发布于 2024-10-08 04:34:30 字数 888 浏览 9 评论 0原文

我的视觉树中有一个文本框,如下所示。

  • 窗口
    • 网格
      • 列表框
        • 项目模板
          • 数据模板
            • 网格
              • 网格
                • 文本框...
The textbox is defined as..

<TextBox Height="Auto" 
         Text="{Binding Path=LyricsForDisplay}" 
         MinHeight="50" 
         MaxHeight="400"  
         Visibility="Visible" 
         VerticalScrollBarVisibility="Auto" 
         IsReadOnly="True" 
         AllowDrop="False" 
         TextWrapping="WrapWithOverflow">
</TextBox>

当长文本添加到绑定变量(LyricsForDisplay)时,列表框中的所有项目都会扩展其文本框/网格宽度,以允许在使用底部出现的滚动条时看到整个字符串...

我想要什么要做的就是使框/网格仅在用户拉伸窗口时调整大小..而不是在输入长文本时调整大小(它可能会环绕..)

有谁知道如何获得该功能?

I have a textbox in my visual tree as follows..

  • Window
    • Grid
      • ListBox
        • ItemTemplate
          • DataTemplate
            • Grid
              • Grid
                • Textbox...

The textbox is defined as..

<TextBox Height="Auto" 
         Text="{Binding Path=LyricsForDisplay}" 
         MinHeight="50" 
         MaxHeight="400"  
         Visibility="Visible" 
         VerticalScrollBarVisibility="Auto" 
         IsReadOnly="True" 
         AllowDrop="False" 
         TextWrapping="WrapWithOverflow">
</TextBox>

When long text is added to the bound variable (LyricsForDisplay) all of the items in the listbox expand their textboxes/grids width's to allow for the entire string to be seen if you use the scrollbar on bottom that appears...

What I would like to do is make it so the boxes/grids only resize if the user stretches the window .. NOT when a long text is entered (it could just wrap around..)

Does anyone know how to obtain the functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罗罗贝儿 2024-10-15 04:34:31

某些东西需要限制文本框可用的水平宽度,在这种情况下,您希望阻止ListBox无限期地水平增长:

<ListBox HorizontalScrollBarVisibility="Disabled"

Something needs to contrain the horizontal width available to the TextBoxes, in this case you want to stop the ListBox from growing horizontally indefinitely:

<ListBox HorizontalScrollBarVisibility="Disabled"
奈何桥上唱咆哮 2024-10-15 04:34:31

二改代码:
1-添加边框标签与您需要的 grid.column 和 grid.row 大小。
2- 设置文本框的宽度和高度。
样本:

 <Border x:Name="b" Margin="5"/>


  <TextBox Height="Auto" 
             Text="{Binding Path=LyricsForDisplay}" 
             MinHeight="50" 
             Width="{Binding ActualWidth, ElementName=b}" 
             Height="{Binding ActualHeight, ElementName=b}" 
             Visibility="Visible" 
             VerticalScrollBarVisibility="Auto" 
             IsReadOnly="True" 
             AllowDrop="False" 
             TextWrapping="Wrap">
    </TextBox>

two change the code:
1- add border tag with your grid.column and grid.row size that you are neads.
2- width and height of textbox set to it.
sample:

 <Border x:Name="b" Margin="5"/>


  <TextBox Height="Auto" 
             Text="{Binding Path=LyricsForDisplay}" 
             MinHeight="50" 
             Width="{Binding ActualWidth, ElementName=b}" 
             Height="{Binding ActualHeight, ElementName=b}" 
             Visibility="Visible" 
             VerticalScrollBarVisibility="Auto" 
             IsReadOnly="True" 
             AllowDrop="False" 
             TextWrapping="Wrap">
    </TextBox>
断肠人 2024-10-15 04:34:31

尝试在文本框中设置 MaxWidth 属性

<TextBox Height="Auto" Text="{Binding Path=LyricsForDisplay}" MinHeight="50" MaxHeight="400"  Visibility="Visible" VerticalScrollBarVisibility="Auto" MaxWidth="100" IsReadOnly="True" AllowDrop="False" TextWrapping="WrapWithOverflow">                            </TextBox>

Try setting the the MaxWidth Property in Your Textbox

<TextBox Height="Auto" Text="{Binding Path=LyricsForDisplay}" MinHeight="50" MaxHeight="400"  Visibility="Visible" VerticalScrollBarVisibility="Auto" MaxWidth="100" IsReadOnly="True" AllowDrop="False" TextWrapping="WrapWithOverflow">                            </TextBox>
埋情葬爱 2024-10-15 04:34:30

不幸的是,常规 TextBox 不允许自动调整大小以适应父级,但当文本不适合时则不允许自动调整大小。

要解决此问题,您可以使用报告所需 (0, 0) 大小的自定义文本框。这是一个丑陋的黑客,但它有效。

在您的 .xaml.cs 文件中:

public class TextBoxThatDoesntResizeWithText : TextBox
{
    protected override Size MeasureOverride(Size constraint)
    {
        return new Size(0, 0);
    }
}

然后,在您的 .xaml 文件中:

<Window x:Class="YourNamespace.YourWindow"
    ...
    xmlns:local="clr-namespace:YourNamespace">
        ...
        <local:TextBoxThatDoesntResizeWithText Height="Auto" 
                                               Text="{Binding Path=LyricsForDisplay}" 
                                               MinHeight="50" 
                                               MaxHeight="400"  
                                               Visibility="Visible" 
                                               VerticalScrollBarVisibility="Auto" 
                                               IsReadOnly="True" 
                                               AllowDrop="False" 
                                               TextWrapping="WrapWithOverflow">
        </local:TextBoxThatDoesntResizeWithText>
        ...
</Window>

Unfortunately, the regular TextBox doesn't allow autoresize to fit the parent but NOT autoresize when the text doesn't fit.

To solve this problem, you can use a custom TextBox that reports a desired (0, 0) size. It's an ugly hack, but it works.

In your .xaml.cs file:

public class TextBoxThatDoesntResizeWithText : TextBox
{
    protected override Size MeasureOverride(Size constraint)
    {
        return new Size(0, 0);
    }
}

Then, in your .xaml file:

<Window x:Class="YourNamespace.YourWindow"
    ...
    xmlns:local="clr-namespace:YourNamespace">
        ...
        <local:TextBoxThatDoesntResizeWithText Height="Auto" 
                                               Text="{Binding Path=LyricsForDisplay}" 
                                               MinHeight="50" 
                                               MaxHeight="400"  
                                               Visibility="Visible" 
                                               VerticalScrollBarVisibility="Auto" 
                                               IsReadOnly="True" 
                                               AllowDrop="False" 
                                               TextWrapping="WrapWithOverflow">
        </local:TextBoxThatDoesntResizeWithText>
        ...
</Window>
人疚 2024-10-15 04:34:30

以下内容有效:

<ListBox Name="ListBox1"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid>
                    <TextBox TextWrapping="Wrap"></TextBox>
                </Grid>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

请注意 ScrollViewer.Horizo​​ntalScrollBarVisibility="Disabled"TextWrapping="Wrap" 的使用。

The following works:

<ListBox Name="ListBox1"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid>
                    <TextBox TextWrapping="Wrap"></TextBox>
                </Grid>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Notice the use of ScrollViewer.HorizontalScrollBarVisibility="Disabled" and TextWrapping="Wrap".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文