0

I have this WPF app that displays a label, a button and a text box like so:

After adding two lines

After adding three more lines via the button the windwos starts to grow with the textbox:

After adding five lines

Here's the XAML:

<Window x:Class="_99_TestWPFApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Simple WPF Test App" Height="Auto" Width="Auto" SizeToContent="WidthAndHeight">
<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="A LABEL" "/>
    <Button Grid.Row="1" Grid.Column="0" x:Name="btnAdd" Click="btnAdd_Click" Content="ADD LINE" />
    <TextBox  Grid.Row="0" Grid.Column="1"  Grid.RowSpan="2"  VerticalScrollBarVisibility="Visible" Width="150"/>
</Grid>

In order to bind the height of the textbox to the height of the grid, I also added this to the textbox Height="{Binding ElementName=MainGrid, Path=ActualHeight}" but to no effect.

I want the window and grid size to be fixed while the textbox displays its contents via a scrollbar

Is this even possible ? Or do I have to set a fixed height somewhere (e.g. for the window) ?

Angle.Bracket
  • 1,438
  • 13
  • 29
  • Which problem are you going to solve? You have auto width and height for window as well as `SizeToContent` and the same proportions in grid. Your xaml works properly according to declaration – Pavel Anikhouski Jul 06 '20 at 11:12
  • set `MaxHeight` `` – NEBEZ Jul 06 '20 at 11:13
  • If possible, I don't want to set any fixed sizes in the XAML. Imagine I later add add some more controls to the first column of the grid: I'd then have to adapt the fixed size of the text box accordingly. – Angle.Bracket Jul 06 '20 at 11:17
  • https://stackoverflow.com/questions/4465646/wpf-how-to-stop-textbox-from-autosizing maybe this help you. – NEBEZ Jul 06 '20 at 11:28
  • @NEBEZ : this only wraps around horizontal overflows but does not prevent the vertical growth – Angle.Bracket Jul 06 '20 at 11:30

1 Answers1

1

It is actually possible: after some experimentation, I found one possible solution:

<Window x:Class="_99_TestWPFApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Simple WPF Test App" Height="Auto" Width="Auto" SizeToContent="WidthAndHeight">
<Grid x:Name="OuterGrid">
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid x:Name="LeftInnerGrid">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="A LABEL"/>
        <Button Grid.Row="1" Grid.Column="0" x:Name="btnAdd" />
    </Grid>
    <!-- notice the additional grid and the binding of the height here -->
    <Grid Name="RightInnerGrid" Grid.Row="0" Grid.Column="1" Height="{Binding ElementName=OuterGrid, Path=ActualHeight}">
        <TextBox   VerticalScrollBarVisibility="Visible" Width="150"/>
    </Grid>
</Grid>

It was adding the textbox in a grid of its own and binding the inner grid's height property to the outer grid's height that did the trick.

Angle.Bracket
  • 1,438
  • 13
  • 29