Stop TextBox in ScrollViewer from growing with content

user667967

I have a ScrollViewer with HorizontalScrollBarVisibility set to "Auto" that contains a TextBox. The problem is that when a user enters text, the TextBox keeps growing in order to show the entire content. What do I need to change, so that the TextBox only grabs the available width (but is not smaller than a given minimal width)?

The horizontal scroll-bar should only appear if the available horizontal space is not sufficient for the given minimal width.

The TextBox should only grow if there is more horizontal space available.

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*" MinWidth="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" Text="test:"/>
        <TextBox Grid.Column="1"/>
    </Grid>
</ScrollViewer>

The horizontal scrollbar appears even though the MinWidth constrain is fulfilled:

enter image description here

user667967

This seems to be a common problem but I haven't found a satisfying solution on the net.

Here is my solution:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*" MinWidth="100"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" Text="test:"/>

        <local:TextBoxDecorator Grid.Column="1">
            <TextBox Text="content content content content content content"/>
        </local:TextBoxDecorator>
    </Grid>
</ScrollViewer>

c#

public class TextBoxDecorator : Decorator {
    // properties
    public override UIElement Child {
        get {
            return base.Child;
        }
        set {
            var oldValue = base.Child;

            if (oldValue != null) {
                var binding = BindingOperations.GetBinding(oldValue, FrameworkElement.WidthProperty);
                if ((binding != null) && (binding.Source == this))
                    BindingOperations.ClearBinding(oldValue, FrameworkElement.WidthProperty);
            }

            base.Child = value;

            if ((value != null) &&
                BindingOperations.GetBinding(value, FrameworkElement.WidthProperty) == null)
                BindingOperations.SetBinding(
                    value,
                    FrameworkElement.WidthProperty,
                    new Binding() {
                        Source = this,
                        Path = new PropertyPath(FrameworkElement.ActualWidthProperty),
                        Mode = BindingMode.OneWay
                    });
        }
    }

    // methods
    protected override Size MeasureOverride(Size constraint) {
        Size result = base.MeasureOverride(constraint);

        if (double.IsInfinity(constraint.Width))
            result.Width = (Child as FrameworkElement)?.MinWidth ?? 0.0;

        return result;
    }
}

Let me know if this was helpful or if you have any feedback.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to prevent span from growing with content

분류에서Dev

Removing Text From A TextBox

분류에서Dev

WPF Scrollviewer의 Textbox 컨트롤에서 스 와이프 스크롤 사용

분류에서Dev

ScrollViewer의 TextBox가 콘텐츠와 함께 커지는 것을 중지

분류에서Dev

Sending a calculated number from textbox 1 to textbox 2

분류에서Dev

Validation of a textbox, so that the value differed from another textbox

분류에서Dev

How to stop user from login?

분류에서Dev

Stop service loop from activity

분류에서Dev

How to stop add content_flymake.xml to odt exports

분류에서Dev

Trigger a function on selection of text from textbox

분류에서Dev

Try to write from the background worker to a textbox in the GUI

분류에서Dev

Remove last character from textbox on button click

분류에서Dev

jquery autocomplete - selected value disappears from textbox

분류에서Dev

Preventing a user from entering anything but integers into a textbox

분류에서Dev

Memory keeps growing

분류에서Dev

LongListSelector ItemTemplate not growing

분류에서Dev

JS Gauge with growing arc

분류에서Dev

How to stop a remote desktop session from ending

분류에서Dev

How to stop table from adjusting width

분류에서Dev

jquery stop tabs from changing url

분류에서Dev

Stop Fancybox from loading the <head> on ajax calls

분류에서Dev

Is it required to explicitly stop an Akka actor from supervisor?

분류에서Dev

Stop Dell from throttling CPU with power adapter

분류에서Dev

Stop broken NFS mounts from locking a directory?

분류에서Dev

Stop rails from including the record in an array

분류에서Dev

Is it possible to stop output from a command after bg?

분류에서Dev

ScrollViewer not showing C#

분류에서Dev

Make a Scrollviewer not vertically scrollable

분류에서Dev

ScrollViewer scroll to specific control

Related 관련 기사

뜨겁다태그

보관