WPF DataGrid 分组数据
WPF DataGrid grouping datas
这是我在网上找到的一篇关于在 WPF Datagrid 中合并数据的文章:
Grouping in DataGrid in WPF
我想在 WPF 应用程序中使用来自 Entity Framework DataContext
的数据对其进行测试。
首先我想检查 DataGrid
中的数据是否正确执行。
但是 DataGrid
保持为空。
这里可能 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>
我的.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;
}
}
和我的Entity Frameworkclass
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; }
}
和数据网格中的结果
您可以如下更改 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);
});
}
}
在 XAML 中添加 Loaded
事件:
<UserControl ...
d:DesignHeight="450" d:DesignWidth="800"
Loaded="Window_Loaded">
<DataGrid x:Name="Pvi_DT">
</DataGrid>
</UserControl>
从 table 加载数据被移至 Loaded
事件处理程序并异步执行。因为加载数据可能需要一些时间。因此,可以实现一些逻辑来显示正在执行加载数据的消息,以向用户指示此过程。
如果您想隐藏某些列(例如 PVI_ID
),可以使用自定义属性:
public class HiddenAttribute : Attribute
{
}
public partial class VW_PVI_2
{
[Hidden]
public int PVI_ID { get; set; }
...
}
然后将 AutoGeneratingColumn
事件处理程序声明添加到 dataGrid
到 XAML:
<DataGrid x:Name="dataGrid1" AutoGeneratingColumn="Pvi_DT_AutoGeneratingColumn"/>
以及隐藏某些列的处理程序声明:
private void Pvi_DT_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyDescriptor is PropertyDescriptor prop && prop.Attributes.OfType<HiddenAttribute>().Any())
{
e.Cancel = true;
}
}
这是我在网上找到的一篇关于在 WPF Datagrid 中合并数据的文章: Grouping in DataGrid in WPF
我想在 WPF 应用程序中使用来自 Entity Framework DataContext
的数据对其进行测试。
首先我想检查 DataGrid
中的数据是否正确执行。
但是 DataGrid
保持为空。
这里可能 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>
我的.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;
}
}
和我的Entity Frameworkclass
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; }
}
和数据网格中的结果
您可以如下更改 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);
});
}
}
在 XAML 中添加 Loaded
事件:
<UserControl ...
d:DesignHeight="450" d:DesignWidth="800"
Loaded="Window_Loaded">
<DataGrid x:Name="Pvi_DT">
</DataGrid>
</UserControl>
从 table 加载数据被移至 Loaded
事件处理程序并异步执行。因为加载数据可能需要一些时间。因此,可以实现一些逻辑来显示正在执行加载数据的消息,以向用户指示此过程。
如果您想隐藏某些列(例如 PVI_ID
),可以使用自定义属性:
public class HiddenAttribute : Attribute
{
}
public partial class VW_PVI_2
{
[Hidden]
public int PVI_ID { get; set; }
...
}
然后将 AutoGeneratingColumn
事件处理程序声明添加到 dataGrid
到 XAML:
<DataGrid x:Name="dataGrid1" AutoGeneratingColumn="Pvi_DT_AutoGeneratingColumn"/>
以及隐藏某些列的处理程序声明:
private void Pvi_DT_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyDescriptor is PropertyDescriptor prop && prop.Attributes.OfType<HiddenAttribute>().Any())
{
e.Cancel = true;
}
}