c# - WPF DataGrid 扩展到 Grid 之外

标签 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");
        }
    }
}

我已经尝试在每个网格的最后一行使用“自动”而不是“*”,但没有帮助。我需要保留此网格结构以维护 Material Design FAB 位置和适当的大小调整。 任何帮助将不胜感激!

最佳答案

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

关于c# - WPF DataGrid 扩展到 Grid 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366626/

相关文章:

wpf - 将Click事件添加到PowerShell中的WPF DataGrid按钮列中

wpf datagrid 数据绑定(bind)与嵌套对象(如主细节)

c# - 如何根据单元格背景颜色更改 WPF DataGrid 单元格小部件背景颜色?

c# - 检查电子邮件是否存在,自动重新连接 tcpclient c#

c# - 从数据库馈送的 ListBox 中获取 SelectedItems

c# - 您如何命名 xSpecification/BDD 测试类以便它们传达意图?特别是在解决方案资源管理器中

使用 MediaElement 的 WPF 动画 GIF

c# - 松散的 XAML 是根据引用的命名空间加载的 .NET/CLR 版本吗?

c# - EventWaithandle 与 while(true) Thread.Sleep

c# - 当事件网格在针对其订阅者的事件传递失败时发送电子邮件通知