WPF - MeasureOverride - TextBox does not autosize anymore

58 Views Asked by At

I have encountered a rather interesting problem. One of my user controls contains a label, a StackPanel which will contain further controls and a TextBox for a comment. The controls in the StackPanel may vary so I've set the Window to AutoSize. Works like a charm, no matter which controls are inside the Panel the Window is sized in width to display them all.

Now our UI designer has got the idea that the TextBox should not stretch the control. Meaning if there is a long text inside it it should not stretch the Window but just wrap. Making the StackPanel the control to determine the Window size.

So I've overwritten the MeasureOverride method on the user control to not add the TextBox width. Seems to be working on first glance. But the Window does not grow properly anymore. Press enter and the Window does not get adjusted to display the now bigger UserControl / TextBox.

I have looked at the Microsoft site and don't really see what I have missed. The reference source for TextBox and StackPanel did not bring enlightenment either.

Any suggestions?

UserControl xaml:

<UserControl x:Class="MeasureOverrideTest.uc1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="Auto">

    <StackPanel>

        <Label Name="_label" Content="control title ... foo ... bar ..." />
        <StackPanel Name="_panel" Background="Aqua" MinWidth="200" MinHeight="20"></StackPanel>
        <TextBox Name="_textBox" AcceptsReturn="True" MaxLines="4" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"></TextBox>

    </StackPanel>

</UserControl>

UserControl xaml.cs

public partial class uc1 : UserControl
{
    public uc1()
    {
        InitializeComponent();
    }

    protected override Size MeasureOverride(Size constraint)
    {
        var desiredSize = new Size();

        //business as usual
        _label.Measure(constraint);
        desiredSize.Width = Math.Max(desiredSize.Width, _label.DesiredSize.Width);
        desiredSize.Height += _label.DesiredSize.Height;

        //business as usual
        _panel.Measure(constraint);
        desiredSize.Width = Math.Max(desiredSize.Width, _panel.DesiredSize.Width);
        desiredSize.Height += _panel.DesiredSize.Height;

        //do not stretch the control, just take what is there
        _textBox.Measure(constraint); //first try: does not grow in height
        //_textBox.Measure(new Size(desiredSize.Width, constraint.Height)); //second try: not better
        desiredSize.Height += _textBox.DesiredSize.Height;

        //do not exceed the given limits
        desiredSize.Width = Math.Min(desiredSize.Width, constraint.Width);
        desiredSize.Height = Math.Min(desiredSize.Height, constraint.Height);

        return desiredSize;
    }
}

MainWindow:

<Window x:Class="MeasureOverrideTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MeasureOverrideTest"
        SizeToContent="WidthAndHeight">

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <local:uc1 Grid.Row="0"/>
        <StackPanel Grid.Row="1">
            <Button Content="buttons and stuff ...."/>
        </StackPanel>

    </Grid>

</Window>
0

There are 0 best solutions below