WPF文本框和滚动行为 [英] WPF TextBox and Scroll behavior

查看:593
本文介绍了WPF文本框和滚动行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题. 我需要在ScrollViewer中使用控件托管网格,以防止文本框在UI处被截断或折叠为零.我也希望当用户更改窗口宽度时扩展文本框的with. 我将Window的内容设置为以下代码

I have a problem. I need to host grid with controls in ScrollViewer to prevent textbox from being either truncated or collapsed to zero-with at the UI. Also I want the with of textbox to be expanded when user change windows width. I'm setting Window's content to following code

<DockPanel>
    <TreeView DockPanel.Dock="Left" Width="150"/>
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Text="Name" 
                       Margin="5" 
                       VerticalAlignment="Center"/>
            <TextBox Grid.Column="1"
                     Text="Some Name"
                     Margin="5"
                     VerticalAlignment="Center"
                     MinWidth="200"/>
        </Grid>
    </ScrollViewer>
</DockPanel>

一切正常,但是当用户在TextBox中键入很长的文本时,它将被展开并出现水平滚动. 有什么简单的方法可以限制TextBox的最大宽度,并仅在用户更改窗口大小时才允许其扩展.

All work fine, but when user types very long text in TextBox it is being expanded and horizontal scroll appears. Is there any easy way to limit TextBox maximum width and allow it to be expanded only when user changes window size.

推荐答案

问题是父元素为TextBox提供了它认为需要的空间,当显示更多文本时,它将扩展而不是停留在初始自动尺寸.

The problem is that the parent elements are providing TextBox with as much space as it thinks it needs, and when more text is present it will expand instead of staying at the initial automatic size.

这里的一种解决方案是制作另一个自动调整大小的元素,并将TextBox.Width绑定到该元素:

One solution here is to make another auto-sized element and bind the TextBox.Width to it:

<DockPanel>
    <TreeView Width="150" DockPanel.Dock="Left"/>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Margin="5" VerticalAlignment="Center" Text="Name"/>
            <Border x:Name="b" Grid.Column="1" Margin="5"/>
            <TextBox Width="{Binding ActualWidth, ElementName=b}"
                     MinWidth="200"
                     Grid.Column="1"
                     Margin="5"
                     VerticalAlignment="Center"
                     Text="Some Name"/>
        </Grid>
    </ScrollViewer>
</DockPanel>

请注意,我们设置了自动调整大小元素(边框)的Margin属性.这很重要,因为如果未设置,则会出现循环:

Note that we set the Margin property of the auto-sizing element (the Border). This is important because if it's not set, there will be a loop:

  1. 边框宽度会自动调整为网格"列的宽度
  2. 文本框的宽度调整为Border.ActualWidth
  3. 网格列的宽度调整为TextBox宽度+ TextBox边距
  4. 边框宽度会再次自动调整为网格"列的宽度

通过将Margin设置为与TextBox相同,调整TextBox的大小不会影响网格大小.

By setting the Margin to the same as the TextBox, the resizing of the TextBox won't affect the Grid size.

这篇关于WPF文本框和滚动行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆