简体   繁体   English

WPF DataGrid 分组数据

[英]WPF DataGrid grouping datas

Here's an article I found on the web to hroupe datas in a WPF Datagrid: Grouping in DataGrid in WPF这是我在 web 上找到的一篇文章,用于整理 WPF 数据网格中的数据:WPF 中的数据网格中的分组

I want to test it in a WPF application with data coming from an Entity Framework DataContext .我想在 WPF 应用程序中使用来自 Entity Framework DataContext的数据对其进行测试。 First I want to check that data are correctly implemented in the DataGrid .首先,我想检查DataGrid中的数据是否正确实现。 But the DataGrid stays empty.DataGrid保持为空。 Here's may xaml :这里可能是 xaml

 <UserControl x:Class="GESTION_CONGELATION_V2.User_Controls.ViewPvi_UC" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <DataGrid x:Name="Pvi_DT"> </DataGrid> </UserControl>

My .cs我的.cs

public partial class ViewPvi_UC : UserControl
{
    ObservableCollection<VW_PVI_2> pvi = new ObservableCollection<VW_PVI_2>();
    public ViewPvi_UC()
    {
        InitializeComponent();
        ObservableCollection<VW_PVI_2> pvi = new ObservableCollection<VW_PVI_2>();
        Pvi_DT.ItemsSource = pvi;
    }
}

and my Entity Framework class和我的实体框架 class

public partial class VW_PVI_2
{
    public int PVI_ID { get; set; }
    public string VE_NOM { get; set; }
    public string VD_VIRTUAL { get; set; }
    public string VE_OLD_NAME { get; set; }
    public string ST_LIB { get; set; }
    public string VE_COMM { get; set; }
    public string IMP_NOM { get; set; }
    public string IMP_IP { get; set; }
    public string IMP_MAC { get; set; }
    public string ST_LIB_IMP { get; set; }
    public string IMP_COMM { get; set; }
    public string SERV_NOM { get; set; }
    public string PO_NOM { get; set; }
}

and the result in the datagrid和数据网格中的结果在此处输入图像描述

You can change your the pvi declaration as following, to use the ICollectionView :您可以按如下方式更改您的pvi声明,以使用ICollectionView

public partial class ViewPvi_UC : UserControl
{
    public ICollectionView pvi { get; set; }
    public ViewPvi_UC()
    {
        InitializeComponent();      
    }

    async private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        await Task<object>.Factory.StartNew(() =>
        {
            // Select all `VW_PVI_2` from your table here to be populate the `pvi`
            IList<VW_PVI_2> data= YourDbContext.YourTable.Select(r => r).ToList();
            pvi = new ObservableCollection<VW_PVI_2>(data);
            this.Dispatcher.Invoke(() => {  Pvi_DT.ItemsSource = pvi; });
            return Task.FromResult<object>(null);
        });
    }
}

Adding the Loaded event in the XAML:在 XAML 中添加Loaded事件:

<UserControl ...
    d:DesignHeight="450" d:DesignWidth="800"
    Loaded="Window_Loaded">
    <DataGrid x:Name="Pvi_DT">
        
    </DataGrid>
</UserControl>

Loading the data from a table is moved to the Loaded event handler and performing asynchronously.从表中加载数据被移至Loaded事件处理程序并异步执行。 Because of loading data can take some time.因为加载数据可能需要一些时间。 Therefore is is possible to implement some logic to show a message that loading data is performing to indicate the user about this process.因此,可以实现一些逻辑来显示正在执行加载数据的消息,以指示用户有关此过程的信息。

In case you want to hide some columns (for example PVI_ID ) it is possible to use a custom attribute:如果您想隐藏某些列(例如PVI_ID ),可以使用自定义属性:

public class HiddenAttribute : Attribute
{
}

public partial class VW_PVI_2
{
    [Hidden]
    public int PVI_ID { get; set; }
    ...
}

Then add the AutoGeneratingColumn event handler declaration to the dataGrid to the XAML:然后将AutoGeneratingColumn事件处理程序声明添加到dataGrid到 XAML:

<DataGrid x:Name="dataGrid1" AutoGeneratingColumn="Pvi_DT_AutoGeneratingColumn"/>

And the handler declaration to hide some columns:以及隐藏某些列的处理程序声明:

private void Pvi_DT_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyDescriptor is PropertyDescriptor prop && prop.Attributes.OfType<HiddenAttribute>().Any())
    {
        e.Cancel = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM