WPF DataGrid超越了Grid

时间:2016-02-12 15:44:32

标签: c# wpf datagrid

一直在看它4个小时,伙计们......我只能解决这个问题(可能是微软的错误?)

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

https://goo.gl/photos/YnApkZS7v3uZ4TWX6

由于此代码使用的是Material Design库,因此我将其删除为基础知识,以便任何人都可以自己试用。

<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#:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace EvolvDirectoryCreeper
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private ObservableCollection<string> m_badFoldersHistory = new ObservableCollection<string>();
        public ObservableCollection<string> BadFoldersHistory
        {
            get { return m_badFoldersHistory; }
            set { 
                m_badFoldersHistory = value;                                                                           
                OnPropertyChanged("BadFoldersHistory");
            }
        }

        public virtual event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        private Dispatcher MainWindowDispatcher;

        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
            MainWindowDispatcher = Dispatcher;

            for (int i = 0; i < 40; i++)
                BadFoldersHistory.Add("d");
        }
    }
}

我已经尝试了#34; Auto&#34;而不是&#34; *&#34;在每个网格的最后一行,但没有帮助。我需要保持此Grid结构以维护Material Design FAB位置并正确调整大小。 非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

你需要在某处定义高度。我将第一个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>

答案 1 :(得分:0)

一般的解决方案是放置一个控件,如果超出父控件的高度,可以显示content。这是WPF中ContentControl类似控件(如WindowContentControl或任何Panel等)的常见问题。

See this link to see the generic problem

  

我们无法确定内部控制的高度,所以一定要解决   高度解决方案不是最佳解决方案。如果控制高度超过   或匹配grid的高度,然后修复高度解决方案就是kaput。

通用解决方案包含可以处理可变高度的控件中的grid(例如 Scrollviewer )。如下所示:

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

并且您不必担心从grid/Window的高度超过任何控件的高度。

<强>输出:

ScrollViewer