Browse Source

报表设计

样式分支
pry 2 years ago
parent
commit
a49dc1ab39
21 changed files with 747 additions and 31 deletions
  1. +2
    -1
      BPASmartClient.Business/Plugin/ConfigMgr.cs
  2. +43
    -0
      BPASmartClient.CustomResource/Pages/Model/DataGridColumnAttribute.cs
  3. +12
    -0
      BPASmartClient.CustomResource/Pages/Model/DoNotAutoGenerateDataGridColumnAttribute.cs
  4. +15
    -0
      BPASmartClient.CustomResource/Pages/Model/ReportBase.cs
  5. +53
    -0
      BPASmartClient.CustomResource/Pages/Model/ReportHelper.cs
  6. +79
    -0
      BPASmartClient.CustomResource/Pages/Model/ReportTestModel.cs
  7. +47
    -0
      BPASmartClient.CustomResource/Pages/View/ReportView.xaml
  8. +28
    -0
      BPASmartClient.CustomResource/Pages/View/ReportView.xaml.cs
  9. +63
    -0
      BPASmartClient.CustomResource/Pages/ViewModel/ReportViewModel.cs
  10. +76
    -0
      BPASmartClient.CustomResource/UserControls/DataGridEx.cs
  11. +63
    -29
      BPASmartClient/Control/LogView.xaml
  12. +8
    -0
      DosingSystem/App.xaml.cs
  13. +1
    -1
      WPFDemo/App.xaml
  14. +36
    -0
      WPFDemo/Bb/Class1.cs
  15. +43
    -0
      WPFDemo/Bb/DataGridColumnOrderAttribute.cs
  16. +75
    -0
      WPFDemo/Bb/DataGridEx.cs
  17. +12
    -0
      WPFDemo/Bb/DoNotAutoGenerateDataGridColumnAttribute.cs
  18. +30
    -0
      WPFDemo/Bb/ViewModel.cs
  19. +29
    -0
      WPFDemo/Bb/Window1.xaml
  20. +27
    -0
      WPFDemo/Bb/Window1.xaml.cs
  21. +5
    -0
      WPFDemo/WPFDemo.csproj

+ 2
- 1
BPASmartClient.Business/Plugin/ConfigMgr.cs View File

@@ -51,6 +51,7 @@ namespace BPASmartClient.Business
string JsonString = File.ReadAllText(path);
deviceConfigModelJsons = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
}
else deviceConfigModelJsons = new ObservableCollection<DeviceConfigModelJson>();
if (deviceConfigModelJsons?.Count > 0)
{
ClientId = int.Parse(deviceConfigModelJsons.ElementAt(0).ShopId);
@@ -84,7 +85,7 @@ namespace BPASmartClient.Business
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
configurationBuilder.AddApolloConfiguration(p =>
{
p.AppId = apoid;
p.MetaServer = apolloUri;
p.Namespaces = new List<string>() { "application", namespa };


+ 43
- 0
BPASmartClient.CustomResource/Pages/Model/DataGridColumnAttribute.cs View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.CustomResource.Pages.Model
{
[AttributeUsage(AttributeTargets.Property)]
public class DataGridColumnAttribute : Attribute
{
public static readonly DataGridColumnAttribute Default = new DataGridColumnAttribute(0);

public DataGridColumnAttribute(int index)
{
DataGridColumnValue = index;
}

public virtual int DataGridColumn => DataGridColumnValue;

protected int DataGridColumnValue { get; set; }

public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}

return obj is DataGridColumnAttribute other && other.DataGridColumn == DataGridColumn;
}

public override int GetHashCode()
{
return DataGridColumn.GetHashCode();
}

public override bool IsDefaultAttribute()
{
return Equals(Default);
}
}
}

+ 12
- 0
BPASmartClient.CustomResource/Pages/Model/DoNotAutoGenerateDataGridColumnAttribute.cs View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.CustomResource.Pages.Model
{
public class DoNotAutoGenerateDataGridColumnAttribute : Attribute
{
}
}

+ 15
- 0
BPASmartClient.CustomResource/Pages/Model/ReportBase.cs View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace BPASmartClient.CustomResource.Pages.Model
{
[Serializable]
public class ReportBase : ObservableObject
{
}
}

+ 53
- 0
BPASmartClient.CustomResource/Pages/Model/ReportHelper.cs View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using BPASmartClient.Helper;


namespace BPASmartClient.CustomResource.Pages.Model
{
public class ReportHelper
{
public ObservableCollection<ReportBase> Reports { get; set; } = new ObservableCollection<ReportBase>();

public void Add(ReportBase report)
{
Reports.Add(report);
}

public void Save(string name)
{
DataRecord record = new DataRecord();
record.Save(Reports, name);
}

public void Dispos()
{
Reports.Clear();
}

public ObservableCollection<ReportBase> GetData(string path)
{
DataRecord record = new DataRecord();
return record.Read<ObservableCollection<ReportBase>>(path);
}
}

//public class ReportHelper
//{

// private volatile static ReportHelper _Instance;
// public static ReportHelper GetInstance => _Instance ?? (_Instance = new ReportHelper());
// private ReportHelper() { }

// public object GetData(string path)
// {
// DataRecord record = new DataRecord();
// return record.Read<object>(path);
// }

//}
}

+ 79
- 0
BPASmartClient.CustomResource/Pages/Model/ReportTestModel.cs View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace BPASmartClient.CustomResource.Pages.Model
{
[Serializable]
public class ReportTestModel : ReportBase
{
//[DataGridColumn(0)]//设置列的索引
//[DisplayName("整数")]//设置设备名称
//public int data1 { get { return _mdata1; } set { _mdata1 = value; OnPropertyChanged(); } }
//private int _mdata1;


//[DataGridColumn(1)]
//[DisplayName("字节")]
//public byte data2 { get { return _mdata2; } set { _mdata2 = value; OnPropertyChanged(); } }
//private byte _mdata2;


//[DataGridColumn(2)]
//[DisplayName("布尔")]
//public bool data3 { get { return _mdata3; } set { _mdata3 = value; OnPropertyChanged(); } }
//private bool _mdata3;


//[DataGridColumn(3)]
//[DisplayName("字符串")]
//public string data4 { get { return _mdata4; } set { _mdata4 = value; OnPropertyChanged(); } }
//private string _mdata4;


//[DataGridColumn(4)]
//[DisplayName("浮点数")]
//public float data5 { get { return _mdata5; } set { _mdata5 = value; OnPropertyChanged(); } }
//private float _mdata5;


//[DataGridColumn(5)]
//[DisplayName("无符号整数")]
//public ushort data6 { get { return _mdata6; } set { _mdata6 = value; OnPropertyChanged(); } }
//private ushort _mdata6;

[DataGridColumn(0)]//设置列的索引
[DisplayName("整数")]//设置设备名称
public int data1 { get; set; }


[DataGridColumn(1)]
[DisplayName("字节")]
public byte data2 { get; set; }


[DataGridColumn(2)]
[DisplayName("布尔")]
public bool data3 { get; set; }


[DataGridColumn(3)]
[DisplayName("字符串")]
public string data4 { get; set; }


[DataGridColumn(4)]
[DisplayName("浮点数")]
public float data5 { get; set; }


[DataGridColumn(5)]
[DisplayName("无符号整数")]
public ushort data6 { get; set; }

}
}

+ 47
- 0
BPASmartClient.CustomResource/Pages/View/ReportView.xaml View File

@@ -0,0 +1,47 @@
<UserControl
x:Class="BPASmartClient.CustomResource.Pages.View.ReportView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BPASmartClient.CustomResource.Pages.View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pry="clr-namespace:BPASmartClient.CustomResource.UserControls"
xmlns:vm="clr-namespace:BPASmartClient.CustomResource.Pages.ViewModel"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">

<UserControl.DataContext>
<vm:ReportViewModel />
</UserControl.DataContext>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition />
</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal">
<Button
Width="70"
Margin="5,0"
Command="{Binding AddCommand}"
Content="添加数据" />
<Button
Width="70"
Margin="5,0"
Command="{Binding SaveCommand}"
Content="保存数据" />
<Button
Width="70"
Margin="5,0"
Command="{Binding ReadCommand}"
Content="获取数据" />
</StackPanel>

<Grid Grid.Row="1">
<pry:DataGridEx ItemsSource="{Binding ReportData}" />
</Grid>

</Grid>
</UserControl>

+ 28
- 0
BPASmartClient.CustomResource/Pages/View/ReportView.xaml.cs View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BPASmartClient.CustomResource.Pages.View
{
/// <summary>
/// ReportView.xaml 的交互逻辑
/// </summary>
public partial class ReportView : UserControl
{
public ReportView()
{
InitializeComponent();
}
}
}

+ 63
- 0
BPASmartClient.CustomResource/Pages/ViewModel/ReportViewModel.cs View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using BPASmartClient.CustomResource.Pages.Model;
using Microsoft.Toolkit.Mvvm.Input;
using System.Diagnostics;
using System.IO;

namespace BPASmartClient.CustomResource.Pages.ViewModel
{
public class ReportViewModel
{
ReportHelper reportHelper = new ReportHelper();
public ReportViewModel()
{
AddCommand = new RelayCommand(() =>
{
for (int i = 0; i < 30; i++)
{
ReportData.Add(new ReportTestModel()
{
data1 = new Random().Next(1000, 2000),
data2 = (byte)new Random().Next(0, 240),
data3 = Convert.ToBoolean(new Random().Next(0, 1)),
data4 = new Random().Next(3000, 4000).ToString(),
data5 = (float)new Random().NextDouble() * 100,
data6 = (ushort)new Random().Next(5000, 6000)
});
}
//ReportData = reportHelper.Reports;
});

SaveCommand = new RelayCommand(() =>
{
reportHelper.Save($"aa.pry");
});

ReadCommand = new RelayCommand(() =>
{
ReportData.Clear();
var res = reportHelper.GetData($"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\ProductionData\\aa.pry");
if (res != null)
{
foreach (var item in res)
{
ReportData.Add(item);
}
}
});

}

public ObservableCollection<ReportBase> ReportData { get; set; } = new ObservableCollection<ReportBase>();

public RelayCommand AddCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand ReadCommand { get; set; }
}
}

+ 76
- 0
BPASmartClient.CustomResource/UserControls/DataGridEx.cs View File

@@ -0,0 +1,76 @@
using BPASmartClient.CustomResource.Pages.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace BPASmartClient.CustomResource.UserControls
{
public class DataGridEx : DataGrid
{
private List<ColumnAndOrder> _columnAndOrderList = new List<ColumnAndOrder>();

public DataGridEx()
{
AutoGeneratingColumn += DataGridEx_AutoGeneratingColumn;
AutoGeneratedColumns += DataGridEx_AutoGeneratedColumns;
}

private void DataGridEx_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{

e.Cancel = true;//将创建过程标记为已取消,以阻止DataGrid将生成的列对象加入到列集合中.

//从Attribute中读取显示名称.
var displayName = ((System.ComponentModel.MemberDescriptor)e.PropertyDescriptor).DisplayName;
if (!string.IsNullOrWhiteSpace(displayName))
{
e.Column.Header = displayName;
}

//从Attribute中读取顺序,以及是否自动生成列的标识.
var attributes = ((System.ComponentModel.MemberDescriptor)e.PropertyDescriptor).Attributes;
var order = 0;
foreach (Attribute attribute in attributes)
{
if (attribute is DataGridColumnAttribute orderAttribute)
{
order = orderAttribute.DataGridColumn;
}
else if (attribute is DoNotAutoGenerateDataGridColumnAttribute)
{
//发现 DoNotAutoGenerateDataGridColumnAttribute 时,丢弃已生成的列
return;
}
}

//将创建的列及顺序保存
_columnAndOrderList.Add(new ColumnAndOrder(order, e.Column));
}

private void DataGridEx_AutoGeneratedColumns(object sender, EventArgs e)
{
//按顺序将所有列加入到Columns集合中
foreach (var columnAndOrder in _columnAndOrderList.OrderBy(v => v.Order))
{
Columns.Add(columnAndOrder.DataGridColumn);
}
_columnAndOrderList = null;
}

private class ColumnAndOrder
{
public ColumnAndOrder(int order, DataGridColumn dataGridColumn)
{
Order = order;
DataGridColumn = dataGridColumn;
}

public int Order { get; }

public DataGridColumn DataGridColumn { get; }
}
}
}

+ 63
- 29
BPASmartClient/Control/LogView.xaml View File

@@ -1,50 +1,76 @@
<UserControl x:Class="BPASmartClient.Control.LogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:k="clr-namespace:BPASmartClient.Model;assembly=BPASmartClient.Model"
xmlns:vm="clr-namespace:BPASmartClient.ViewModel;assembly=BPASmartClient.ViewModel"
xmlns:local="clr-namespace:BPASmartClient.Control"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl
x:Class="BPASmartClient.Control.LogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:k="clr-namespace:BPASmartClient.Model;assembly=BPASmartClient.Model"
xmlns:local="clr-namespace:BPASmartClient.Control"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:BPASmartClient.ViewModel;assembly=BPASmartClient.ViewModel"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/GenricStyle.xaml"/>
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/MyStyle.xaml"/>
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/GenricStyle.xaml" />
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/MyStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

<!--底部窗体栏-->
<!-- 底部窗体栏 -->
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition/>
<RowDefinition Height="45" />
<RowDefinition />
</Grid.RowDefinitions>

<!--查询按钮栏-->
<StackPanel Orientation="Horizontal" Margin="10,0,10,0">
<ComboBox Width="100" Text="{Binding SelectedText}" IsEditable="True" ItemsSource="{Binding Path=BookExs}">
<!-- 查询按钮栏 -->
<StackPanel Margin="10,0,10,0" Orientation="Horizontal">
<ComboBox
Width="100"
IsEditable="True"
ItemsSource="{Binding Path=BookExs}"
Text="{Binding SelectedText}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type k:BookEx}">
<CheckBox Width="95" IsChecked="{Binding IsChecked}" Content="{Binding BookN.Name}" />
<CheckBox
Width="95"
Content="{Binding BookN.Name}"
IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Margin="10,0,0,0" Cursor="Hand" Command="{Binding ExcelCommand}" >导出日志</Button>
<Button Margin="10,0,0,0" Cursor="Hand" Command="{Binding OpenCommand}" >打开文件</Button>
<Button
Margin="10,0,0,0"
Command="{Binding ExcelCommand}"
Cursor="Hand">
导出日志
</Button>
<Button
Margin="10,0,0,0"
Command="{Binding OpenCommand}"
Cursor="Hand">
打开文件
</Button>
</StackPanel>

<!--表格栏-->
<!-- 表格栏 -->
<Grid Grid.Row="1">
<DataGrid Margin="10" ItemsSource="{Binding LogDataGrid, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2">
<DataGrid
Grid.Row="2"
Margin="10"
ItemsSource="{Binding LogDataGrid, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="日志时间" Width="300">
<DataGridTemplateColumn Width="300" Header="日志时间">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding time, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14" Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock
HorizontalAlignment="Center"
FontSize="14"
Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding time, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
@@ -57,17 +83,25 @@
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>-->

<DataGridTemplateColumn Header="日志类型" Width="300">
<DataGridTemplateColumn Width="300" Header="日志类型">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14" Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock
HorizontalAlignment="Center"
FontSize="14"
Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="日志内容" Width="*">
<DataGridTemplateColumn Width="*" Header="日志内容">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Text="{Binding message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14" Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock
HorizontalAlignment="Left"
FontSize="14"
Foreground="{Binding foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


+ 8
- 0
DosingSystem/App.xaml.cs View File

@@ -156,6 +156,14 @@ namespace BPASmartClient.DosingSystem
ToggleWindowPath = "Pages.View.DebugLogView"
});

InfoLog.Add(new SubMenumodel()
{
SubMenuName = "报表数据",
SubMenuPermission = new Permission[] { Permission.操作员, Permission.管理员, Permission.技术员, Permission.技术员 },
AssemblyName = "BPASmartClient.CustomResource",
ToggleWindowPath = "Pages.View.ReportView"
});

MenuManage.GetInstance.menuModels.Add(new MenuModel()
{
MainMenuIcon = "&#xe668;",


+ 1
- 1
WPFDemo/App.xaml View File

@@ -3,6 +3,6 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFDemo"
StartupUri="MainWindow.xaml">
StartupUri="Bb/Window1.xaml">
<Application.Resources />
</Application>

+ 36
- 0
WPFDemo/Bb/Class1.cs View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFDemo.Bb
{
public class Class1
{
[DataGridColumnOrder(0)]//设置列的索引
[DisplayName("整数")]//设置设备名称
public int data1 { get; set; }

[DataGridColumnOrder(1)]
[DisplayName("字节")]
public byte data2 { get; set; }

[DataGridColumnOrder(2)]
[DisplayName("布尔")]
public bool data3 { get; set; }

[DataGridColumnOrder(3)]
[DisplayName("字符串")]
public string data4 { get; set; }

[DataGridColumnOrder(4)]
[DisplayName("浮点数")]
public float data5 { get; set; }

[DataGridColumnOrder(5)]
[DisplayName("无符号整数")]
public ushort data6 { get; set; }
}
}

+ 43
- 0
WPFDemo/Bb/DataGridColumnOrderAttribute.cs View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFDemo.Bb
{
[AttributeUsage(AttributeTargets.Property)]
public class DataGridColumnOrderAttribute : Attribute
{
public static readonly DataGridColumnOrderAttribute Default = new DataGridColumnOrderAttribute(0);

public DataGridColumnOrderAttribute(int order)
{
DataGridColumnOrderValue = order;
}

public virtual int DataGridColumnOrder => DataGridColumnOrderValue;

protected int DataGridColumnOrderValue { get; set; }

public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}

return obj is DataGridColumnOrderAttribute other && other.DataGridColumnOrder == DataGridColumnOrder;
}

public override int GetHashCode()
{
return DataGridColumnOrder.GetHashCode();
}

public override bool IsDefaultAttribute()
{
return Equals(Default);
}
}
}

+ 75
- 0
WPFDemo/Bb/DataGridEx.cs View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WPFDemo.Bb
{
public class DataGridEx : DataGrid
{
private List<ColumnAndOrder> _columnAndOrderList = new List<ColumnAndOrder>();

public DataGridEx()
{
AutoGeneratingColumn += DataGridEx_AutoGeneratingColumn;
AutoGeneratedColumns += DataGridEx_AutoGeneratedColumns;
}

private void DataGridEx_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{

e.Cancel = true;//将创建过程标记为已取消,以阻止DataGrid将生成的列对象加入到列集合中.

//从Attribute中读取显示名称.
var displayName = ((System.ComponentModel.MemberDescriptor)e.PropertyDescriptor).DisplayName;
if (!string.IsNullOrWhiteSpace(displayName))
{
e.Column.Header = displayName;
}

//从Attribute中读取顺序,以及是否自动生成列的标识.
var attributes = ((System.ComponentModel.MemberDescriptor)e.PropertyDescriptor).Attributes;
var order = 0;
foreach (Attribute attribute in attributes)
{
if (attribute is DataGridColumnOrderAttribute orderAttribute)
{
order = orderAttribute.DataGridColumnOrder;
}
else if (attribute is DoNotAutoGenerateDataGridColumnAttribute)
{
//发现 DoNotAutoGenerateDataGridColumnAttribute 时,丢弃已生成的列
return;
}
}

//将创建的列及顺序保存
_columnAndOrderList.Add(new ColumnAndOrder(order, e.Column));
}

private void DataGridEx_AutoGeneratedColumns(object sender, EventArgs e)
{
//按顺序将所有列加入到Columns集合中
foreach (var columnAndOrder in _columnAndOrderList.OrderBy(v => v.Order))
{
Columns.Add(columnAndOrder.DataGridColumn);
}
_columnAndOrderList = null;
}

private class ColumnAndOrder
{
public ColumnAndOrder(int order, DataGridColumn dataGridColumn)
{
Order = order;
DataGridColumn = dataGridColumn;
}

public int Order { get; }

public DataGridColumn DataGridColumn { get; }
}
}
}

+ 12
- 0
WPFDemo/Bb/DoNotAutoGenerateDataGridColumnAttribute.cs View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFDemo.Bb
{
public class DoNotAutoGenerateDataGridColumnAttribute : Attribute
{
}
}

+ 30
- 0
WPFDemo/Bb/ViewModel.cs View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
namespace WPFDemo.Bb
{
public class ViewModel : ObservableObject
{
public ViewModel()
{
for (int i = 0; i < 30; i++)
{
class1s.Add(new Class1()
{
data1 = new Random().Next(1000, 2000),
data2 = (byte)new Random().Next(0, 240),
data3 = Convert.ToBoolean(new Random().Next(0, 1)),
data4 = new Random().Next(3000, 4000).ToString(),
data5 = (float)new Random().NextDouble() * 100,
data6 = (ushort)new Random().Next(5000, 6000)
});
}
}

public ObservableCollection<Class1> class1s { get; set; } = new ObservableCollection<Class1>();
}
}

+ 29
- 0
WPFDemo/Bb/Window1.xaml View File

@@ -0,0 +1,29 @@
<Window
x:Class="WPFDemo.Bb.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFDemo.Bb"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
mc:Ignorable="d">

<Window.DataContext>
<local:ViewModel />
</Window.DataContext>

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/GenricStyle.xaml" />
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/MyStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

<Grid>
<local:DataGridEx ItemsSource="{Binding class1s}" />
</Grid>
</Window>

+ 27
- 0
WPFDemo/Bb/Window1.xaml.cs View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFDemo.Bb
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}

+ 5
- 0
WPFDemo/WPFDemo.csproj View File

@@ -12,12 +12,17 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Vlc.DotNet.Core" Version="3.1.0" />
<PackageReference Include="Vlc.DotNet.Core.Interops" Version="3.1.0" />
<PackageReference Include="Vlc.DotNet.Wpf" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.CustomResource\BPASmartClient.CustomResource.csproj" />
</ItemGroup>

<ItemGroup>
<Resource Include="device.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>


Loading…
Cancel
Save