C# WPF DataGrid扩展到了网格之外

C# WPF DataGrid扩展到了网格之外,c#,wpf,datagrid,C#,Wpf,Datagrid,看了4个小时了,伙计们。。。我就是搞不懂(可能是微软的bug?) 这就是我所拥有的,除了DataGrid控件之外,一切都很好。正如您在本视频中看到的,数据超出了应用程序的边界(网格和滚动条): 由于这段代码使用的是材质设计库,所以我将其精简为基本内容,以便任何人都可以自己尝试 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.m

看了4个小时了,伙计们。。。我就是搞不懂(可能是微软的bug?)

这就是我所拥有的,除了DataGrid控件之外,一切都很好。正如您在本视频中看到的,数据超出了应用程序的边界(网格和滚动条):

由于这段代码使用的是材质设计库,所以我将其精简为基本内容,以便任何人都可以自己尝试

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="EvolvDirectoryCreeper.MainWindow"
    Height="700" Width="1000">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto" MinWidth="80"/>
    </Grid.ColumnDefinitions>
    <Grid Height="180" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2">
        <Grid Margin="10,10,100,10">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Grid.Row="0" Margin="4"/>
            <TextBox Grid.Column="0" Grid.Row="1" Margin="4"/>
            <TextBox Grid.Column="0" Grid.Row="2" Margin="4"/>
            <Button Grid.Column="1" Grid.Row="0" Margin="4"/>
            <Button Grid.Column="1" Grid.Row="1" Margin="4"/>
            <Button Grid.Column="1" Grid.Row="2" Margin="4"/>
        </Grid>
    </Grid>
    <Grid Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Height="40" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" Padding="0,15,0,0" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
        <ProgressBar Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" VerticalAlignment="Bottom"/>
        <Label Content="Good" Grid.Column="0" Grid.Row="1" Margin="4,30,0,0"/>
        <Label Content="Bad" Grid.Column="2" Grid.Row="1" Margin="4,30,0,0"/>
        <DataGrid Grid.Column="0" Grid.Row="2"/>
        <DataGrid Grid.Column="2" Grid.Row="2" ItemsSource="{Binding Path=BadFoldersHistory}"/>
    </Grid>
</Grid>

这是C#:

使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用系统诊断;
利用制度全球化;
使用System.IO;
使用System.Linq;
运用系统反思;
使用System.Text.RegularExpressions;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Threading;
命名空间EvolvDirectoryCreeper
{
公共部分类主窗口:窗口,INotifyPropertyChanged
{
私有ObservableCollection m_badFoldersHistory=新ObservableCollection();
公共可观察收集不良文件夹历史记录
{
获取{return m_badFoldersHistory;}
集合{
m_badFoldersHistory=值;
房地产变更(“不良贷款历史”);
}
}
公共虚拟事件属性ChangedEventHandler属性Changed;
公共虚拟void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
专用调度器;
公共主窗口()
{
初始化组件();
DataContext=this;
MainWindowDispatcher=调度程序;
对于(int i=0;i<40;i++)
BadFoldersHistory.添加(“d”);
}
}
}
我已经在每个网格的最后一行尝试了“Auto”而不是“*”,但是没有帮助。我需要保持这种网格结构,以保持材料设计工厂的位置和适当的大小调整。
任何帮助都将不胜感激

需要定义高度的地方。我将第一个RowDef设置为180,因为您的第一个网格是180。请尝试下面的代码

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="180"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto" MinWidth="80"/>
    </Grid.ColumnDefinitions>
    <Grid Height="180" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2">
        <Grid Margin="10,10,100,10">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Grid.Row="0" Margin="4"/>
            <TextBox Grid.Column="0" Grid.Row="1" Margin="4"/>
            <TextBox Grid.Column="0" Grid.Row="2" Margin="4"/>
            <Button Grid.Column="1" Grid.Row="0" Margin="4"/>
            <Button Grid.Column="1" Grid.Row="1" Margin="4"/>
            <Button Grid.Column="1" Grid.Row="2" Margin="4"/>
        </Grid>
    </Grid>
    <Grid Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Height="40" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" Padding="0,15,0,0"
               HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
        <ProgressBar Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" VerticalAlignment="Bottom"/>
        <Label Content="Good" Grid.Column="0" Grid.Row="1" Margin="4,30,0,0"/>
        <Label Content="Bad" Grid.Column="2" Grid.Row="1" Margin="4,30,0,0"/>
        <DataGrid Grid.Column="0" Grid.Row="2"/>
        <DataGrid Grid.Column="2" Grid.Row="2" ItemsSource="{Binding Path=BadFoldersHistory}"  />
    </Grid>
</Grid>

一般的解决方案是放置一个控件,如果超出父控件的高度,则该控件可以显示
内容。WPF中类似
窗口
内容控制
或任何
面板
等的
内容控制是一个非常常见的问题

我们无法始终确定内部控制的高度,因此需要修复 高度解决方案不是最佳解决方案。如果控制高度超过 或者匹配
网格的高度
,则会得到固定高度的解决方案

一个通用的解决方案是将网格包含在一个可以处理可变高度的控件中(比如
Scrollviewer
)。如下图所示:

<ScrollViewer MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=ActualHeight,Mode=OneWay}">
    <Grid>
............
</ScrollViewer>

............
而且您不必担心任何控件的高度会超出
网格/窗口的高度

输出:


是的!就是这样!谢谢顺便说一下,任何对材料设计WPG库感兴趣的人,我强烈建议:
<ScrollViewer MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=ActualHeight,Mode=OneWay}">
    <Grid>
............
</ScrollViewer>