WPF: GroupBox dynamic height

老子叫甜甜 提交于 2019-12-12 08:40:52

问题


I have a textbox and datagrid inside of a dockpanel that is in a WPF groupbox.

<GroupBox Margin="8,142.04,1.783,230.4" Height="Auto" Header="Desired Meeting Outcomes (decisions or actions)?" MaxWidth="635" MinWidth="550" FontWeight="Bold" FontSize="13.333" BorderBrush="#FFD5DFE5" MinHeight="106" VerticalContentAlignment="Stretch">
        <DockPanel Margin="0">
            <local:TextboxControl Margin="0" d:LayoutOverrides="Height, HorizontalMargin" Width="538.217" VerticalAlignment="Top" HorizontalAlignment="Left" DockPanel.Dock="Top"/>
            <local:  Height="Auto" HorizontalAlignment="Left" MinHeight="25" MinWidth="538" DockPanel.Dock="Top"/>
        </DockPanel>
    </GroupBox>

I am adding rows in the datagrid dynmaically from the textbox causing the datagrid to grow. However, my groupbox's height is not growing dynamically even though its height is set to Auto. How can I get my groupbox to grow and shrink based upon the size of the contents that it holds?


回答1:


You have margins set on all 4 sides with a VerticalAlignment of Stretch. In a Grid this will basically give you a GroupBox that sizes with its parent but not its content. Remove the margin from the right and bottom and change the VerticalAlignment to Top.

The margins are the order of L, T, R, B. So zero out the last two. Height=Auto and VerticalContentAlignment=Stretch are the defaults so you can get rid of those too. Try to keep the XAML as clean as possible.

It's clear from the markup that you're using Blend or Visual Studio's designer. I would suggest using the designer for "preview" mode rather than editing. Although it's gotten much better I find the layout behavior of the designer in both products to be very frustrating. Getting familiar with creating XAML by hand pays dividends in the long run.

EXAMPLE

As per the comments, I'm adding an example of how you would have a DataGrid that causes its parent elements to automatically grow based on height. Notice that only the Window itself has a fixed size. For a Window, if you wanted to make it grow based on height you could set SizeToContent=Height. Notice how you only need to set VerticalAlignment=Top on the outermost element.

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="640" Height="480">
    <Grid x:Name="LayoutRoot" Background="Green" VerticalAlignment="Top">
        <Border Margin="5" BorderBrush="Yellow" BorderThickness="4">
            <GroupBox Header="Data Grid" Background="Orange">
                <DataGrid x:Name="dg" AutoGenerateColumns="True" />
            </GroupBox>
        </Border>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow( )
    {
        InitializeComponent( );

        var items = new ObservableCollection<DateTime>( );
        dg.ItemsSource = items;

        var timer = new DispatcherTimer( );
        timer.Interval = TimeSpan.FromSeconds( 2 );
        timer.Tick += ( s, e ) => items.Add( DateTime.Now );
        timer.Start( );
    }
}



回答2:


What is the container of the GroupBox ? It could prevent it from growing.

For example, if the container is the Windows, does it have SizeToContent="Height" ?



来源:https://stackoverflow.com/questions/2346283/wpf-groupbox-dynamic-height

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!