|
|
@@ -33,23 +33,34 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
public TheDataGrid() |
|
|
|
{ |
|
|
|
InitializeComponent(); |
|
|
|
ResourceDictionary languageResDic = new ResourceDictionary(); |
|
|
|
languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); |
|
|
|
this.Resources.MergedDictionaries.Add(languageResDic); |
|
|
|
//MinWidth = 100; |
|
|
|
//MinHeight = 100; |
|
|
|
//Width = 100; |
|
|
|
//Height = 100; |
|
|
|
//ResourceDictionary languageResDic = new ResourceDictionary(); |
|
|
|
//languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute); |
|
|
|
//this.Resources.MergedDictionaries.Add(languageResDic); |
|
|
|
MinWidth = 100; |
|
|
|
MinHeight = 100; |
|
|
|
Width = 100; |
|
|
|
Height = 100; |
|
|
|
this.Loaded += TheDataGrid_Loaded; ; |
|
|
|
} |
|
|
|
|
|
|
|
public DataSouceModel ItemsString |
|
|
|
private void TheDataGrid_Loaded(object sender, RoutedEventArgs e) |
|
|
|
{ |
|
|
|
get { return (DataSouceModel)GetValue(ItemsStringProperty); } |
|
|
|
set { SetValue(ItemsStringProperty,value); } |
|
|
|
TabItems.CollectionChanged += TabItems_CollectionChanged; |
|
|
|
} |
|
|
|
|
|
|
|
public static readonly DependencyProperty ItemsStringProperty = |
|
|
|
DependencyProperty.Register("ItemsString",typeof(DataSouceModel),typeof(TheDataGrid),new PropertyMetadata(new DataSouceModel())); |
|
|
|
private void TabItems_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
|
|
|
{ |
|
|
|
if (TabItems.Count > 0) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
TabItemsStr = JsonConvert.SerializeObject(TabItems); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public string ControlType => "控件"; |
|
|
|
|
|
|
@@ -67,12 +78,24 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
DispatcherTimer timer = new DispatcherTimer(); |
|
|
|
/// <summary> |
|
|
|
/// 注册需要处理的事件 |
|
|
|
/// </summary> |
|
|
|
public void Register() |
|
|
|
{ |
|
|
|
if (!string.IsNullOrEmpty(TabItemsStr)) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
TabItems = JsonConvert.DeserializeObject<ObservableCollection<列显示设置>>(TabItemsStr); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (DataSouceType == DataTypeEnum.API接口) |
|
|
|
{ |
|
|
@@ -95,6 +118,22 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
|
|
|
|
|
|
|
|
#region 数据绑定模块 |
|
|
|
[Category("名称[自动生成]")] |
|
|
|
private string TabItemsStr |
|
|
|
{ |
|
|
|
get { return (string)GetValue(TabItemsStrProperty); } |
|
|
|
set { SetValue(TabItemsStrProperty, value); } |
|
|
|
} |
|
|
|
private static readonly DependencyProperty TabItemsStrProperty = |
|
|
|
DependencyProperty.Register("TabItemsStr", typeof(string), typeof(TheDataGrid), new PropertyMetadata(string.Empty)); |
|
|
|
[Category("集合")] |
|
|
|
public ObservableCollection<列显示设置> TabItems |
|
|
|
{ |
|
|
|
get { return (ObservableCollection<列显示设置>)GetValue(TabItemsProperty); } |
|
|
|
set { SetValue(TabItemsProperty, value); } |
|
|
|
} |
|
|
|
private static readonly DependencyProperty TabItemsProperty = |
|
|
|
DependencyProperty.Register("TabItems", typeof(ObservableCollection<列显示设置>), typeof(TheDataGrid), new PropertyMetadata(new ObservableCollection<列显示设置>())); |
|
|
|
public event EventHandler PropertyChange; //声明一个事件 |
|
|
|
[Category("数据绑定-数据来源")] |
|
|
|
public DataTypeEnum DataSouceType |
|
|
@@ -119,9 +158,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
set { SetValue(DataSouceInformationProperty,value); } |
|
|
|
} |
|
|
|
public static readonly DependencyProperty DataSouceInformationProperty = |
|
|
|
DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty)); |
|
|
|
|
|
|
|
|
|
|
|
DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata("api0")); |
|
|
|
[Category("数据绑定")] |
|
|
|
public string FDataSouce |
|
|
|
{ |
|
|
@@ -173,14 +210,17 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
//Dictionary<string, object> keys= JsonConvert.DeserializeObject<Dictionary<string, object>>(GenerateData); |
|
|
|
//List<Dictionary<string, object>> obj2 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(keys["data"].ToString()); |
|
|
|
ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData); |
|
|
|
Dictionary<string, object> keys= JsonConvert.DeserializeObject<Dictionary<string, object>>(GenerateData); |
|
|
|
List<Dictionary<string, object>> obj2 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(keys["data"].ToString()); |
|
|
|
//ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData); |
|
|
|
// 运行时进行项目绑定 |
|
|
|
Binding binding = new Binding(); |
|
|
|
binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; |
|
|
|
binding.Path = new PropertyPath("ItemsString.data"); |
|
|
|
SetBinding(ItemsSourceProperty,binding); |
|
|
|
//this.AutoGenerateColumns = false; |
|
|
|
this.CanUserAddRows = false; |
|
|
|
this.CanUserDeleteRows = false; |
|
|
|
this.RowHeaderWidth = 0; |
|
|
|
this.GridLinesVisibility = DataGridGridLinesVisibility.None; |
|
|
|
//this.BorderThickness = new Thickness(0); |
|
|
|
this.ItemsSource = ListToDataTable(obj2).DefaultView; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
@@ -198,6 +238,8 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
{ |
|
|
|
if (arrayList.Count > 0) |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
foreach (Dictionary<string, object> dictionary in arrayList) |
|
|
|
{ |
|
|
|
if (dictionary.Keys.Count<string>() == 0) |
|
|
@@ -205,20 +247,31 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
result = dataTable; |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
if (TabItems == null || TabItems.Count == 0) |
|
|
|
{ |
|
|
|
foreach (string current in dictionary.Keys) |
|
|
|
{ |
|
|
|
TabItems.Add(new 列显示设置 { 列标题 = current, 是否显示 = true, 显示名称 = current }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//在第一次循环时确定datatable的列名,后续循环无需再更改 |
|
|
|
if (dataTable.Columns.Count == 0) |
|
|
|
{ |
|
|
|
//此处仅考虑一层json字符串的形式,多层结构需要深入得到叶子节点的key |
|
|
|
foreach (string current in dictionary.Keys) |
|
|
|
foreach (var current in TabItems) |
|
|
|
{ |
|
|
|
dataTable.Columns.Add(current);//, dictionary[current].GetType()); |
|
|
|
if(current.是否显示) |
|
|
|
dataTable.Columns.Add(current.显示名称);//, dictionary[current].GetType()); |
|
|
|
} |
|
|
|
} |
|
|
|
//每次循环增加datatable的Rows |
|
|
|
DataRow dataRow = dataTable.NewRow(); |
|
|
|
foreach (string current in dictionary.Keys) |
|
|
|
foreach (var current in TabItems) |
|
|
|
{ |
|
|
|
dataRow[current] = dictionary[current]; |
|
|
|
if (current.是否显示) |
|
|
|
dataRow[current.显示名称] = dictionary[current.列标题]; |
|
|
|
} |
|
|
|
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中 |
|
|
|
} |
|
|
@@ -235,12 +288,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public class DataSouceModel |
|
|
|
public class 列显示设置 |
|
|
|
{ |
|
|
|
public List<object> data {get; set; } |
|
|
|
public DataSouceModel() |
|
|
|
{ |
|
|
|
data = new List<object>(); |
|
|
|
} |
|
|
|
public string 列标题 { get; set; } |
|
|
|
public string 显示名称 { get; set; } |
|
|
|
public bool 是否显示 { get; set; } |
|
|
|
} |
|
|
|
} |