添加数据时WPF DataGrid的高度增加 [英] Height of WPF DataGrid increases when data added

查看:119
本文介绍了添加数据时WPF DataGrid的高度增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF DataGrid,当我向其中添加不适合其初始高度的数据时,其高度会增加.我不希望高度发生变化,除非用户增加窗口大小.有没有办法停止这种自动调整大小?

I have a WPF DataGrid that increases in height when I add data to it that won't fit inside its initial height. I don't want the height to change unless the user increases the Window size. Is there a way to stop this auto-resize?

<Window x:Class="WpfDataGridSizeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Loaded="Window_Loaded"
        SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <DataGrid x:Name="wordsDataGrid" VerticalAlignment="Top" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Grid>
</Window>

    public partial class MainWindow : Window
    {
        MyList myList = new MyList();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            wordsDataGrid.DataContext = myList;
            for (int i = 0; i < 100; ++i)
            {
                myList.AddOne("blah blah");
            }
        }
    }

    public class MyList : ObservableCollection<AClass>
    {
        public MyList() { }

        public void AddOne(string aString)
        {
            base.Add(new AClass(aString));
        }
    }

    public class AClass
    {
        public string AString { get; set; }

        public AClass(string aString)
        {
            AString = aString;
        }
    }

推荐答案

如果我没弄错的话,您希望您的DataGrid最初处于半高,在Windows内DataGrid下方有一些空白空间...然后,当调整窗口大小时,DataGrid将更改其大小.

If I don't get you wrong, you want your DataGrid to be in centain Height initally, with some Empty Space below the DataGrid within the Windows...Then when resize the Window, the DataGrid will changes it size.

再向网格中添加一行,并定义该行的MinHeight.然后将DataGrid设置为VerticalAlignment = Stretch.还要为窗口"设置默认的高度尺寸.

Add one more Row to the Grid, and define a MinHeight of that Row. Then set DataGrid to be VerticalAlignment = Stretch. Also set a default height size for the Window.

<Window x:Class="WpfDataGridSizeTest.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Loaded="Window_Loaded" Height="300"> 
    <Grid> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="*"/> 
            <RowDefinition MinHeight="100"/> 
        </Grid.RowDefinitions> 
        <DockPanel Grid.Row="0" VerticalAlignment="Stretch"> 
            <DataGrid x:Name="wordsDataGrid" VerticalAlignment="Stretch" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" > 
                <DataGrid.Columns> 
                    <DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/> 
                </DataGrid.Columns> 
            </DataGrid> 
        </DockPanel> 
    </Grid> 
</Window> 

这篇关于添加数据时WPF DataGrid的高度增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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