准备做控件序列化工作
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
using static Ramitta.winDataGrid;
|
||||
|
||||
namespace Ramitta
|
||||
@@ -26,7 +28,6 @@ namespace Ramitta
|
||||
Rows = new();
|
||||
xDataGrid.ItemsSource = Rows;
|
||||
}
|
||||
|
||||
// 定义列类型的枚举
|
||||
public enum ColumnType
|
||||
{
|
||||
@@ -36,7 +37,6 @@ namespace Ramitta
|
||||
TextBox,
|
||||
Label
|
||||
}
|
||||
|
||||
public void InitColumns(params (string columnName, ColumnType columnType)[] columns)
|
||||
{
|
||||
// 循环添加列
|
||||
@@ -153,5 +153,205 @@ namespace Ramitta
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public JObject JsonPrint()
|
||||
{
|
||||
JObject result = new JObject();
|
||||
JArray rowsArray = new JArray();
|
||||
|
||||
// 遍历所有行
|
||||
foreach (var row in Rows)
|
||||
{
|
||||
JObject rowObject = new JObject();
|
||||
|
||||
// 遍历行中的所有列
|
||||
foreach (var column in row)
|
||||
{
|
||||
string columnName = column.Key;
|
||||
object value = column.Value;
|
||||
|
||||
// 根据控件类型序列化不同的属性
|
||||
switch (ColumnsName[columnName])
|
||||
{
|
||||
case ColumnType.Button:
|
||||
if (value is Button button)
|
||||
rowObject[columnName] = button.Content?.ToString() ?? "";
|
||||
break;
|
||||
|
||||
case ColumnType.CheckBox:
|
||||
if (value is CheckBox checkBox)
|
||||
rowObject[columnName] = checkBox.IsChecked ?? false;
|
||||
break;
|
||||
|
||||
case ColumnType.ComboBox:
|
||||
if (value is ComboBox comboBox)
|
||||
{
|
||||
JObject comboObj = new JObject
|
||||
{
|
||||
["SelectedValue"] = comboBox.SelectedValue?.ToString(),
|
||||
["SelectedIndex"] = comboBox.SelectedIndex,
|
||||
["ItemsCount"] = comboBox.Items?.Count ?? 0
|
||||
};
|
||||
rowObject[columnName] = comboObj;
|
||||
}
|
||||
break;
|
||||
|
||||
case ColumnType.TextBox:
|
||||
if (value is TextBox textBox)
|
||||
{
|
||||
JObject textBoxObj = new JObject
|
||||
{
|
||||
["Text"] = textBox.Text,
|
||||
["Background"] = textBox.Background?.ToString()
|
||||
};
|
||||
rowObject[columnName] = textBoxObj;
|
||||
}
|
||||
break;
|
||||
|
||||
case ColumnType.Label:
|
||||
if (value is Label label)
|
||||
{
|
||||
JObject labelObj = new JObject
|
||||
{
|
||||
["Content"] = label.Content?.ToString(),
|
||||
["Background"] = label.Background?.ToString()
|
||||
};
|
||||
rowObject[columnName] = labelObj;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rowsArray.Add(rowObject);
|
||||
}
|
||||
|
||||
// 添加列信息
|
||||
JObject columnsInfo = new JObject();
|
||||
foreach (var column in ColumnsName)
|
||||
{
|
||||
columnsInfo[column.Key] = column.Value.ToString();
|
||||
}
|
||||
|
||||
result["Columns"] = columnsInfo;
|
||||
result["Rows"] = rowsArray;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void JsonLoad(string jsonString)
|
||||
{
|
||||
try
|
||||
{
|
||||
JObject jsonData = JObject.Parse(jsonString);
|
||||
|
||||
// 清空现有数据
|
||||
Clear();
|
||||
xDataGrid.Columns.Clear();
|
||||
ColumnsName.Clear();
|
||||
|
||||
// 加载列信息
|
||||
JObject columnsInfo = (JObject)jsonData["Columns"];
|
||||
foreach (var column in columnsInfo.Properties())
|
||||
{
|
||||
string columnName = column.Name;
|
||||
ColumnType columnType = (ColumnType)Enum.Parse(typeof(ColumnType), column.Value.ToString());
|
||||
ColumnsName.Add(columnName, columnType);
|
||||
AddColumn(columnName, columnType);
|
||||
}
|
||||
|
||||
// 加载行数据
|
||||
JArray rowsArray = (JArray)jsonData["Rows"];
|
||||
foreach (JObject rowObject in rowsArray)
|
||||
{
|
||||
Dictionary<string, object> newRow = AddRow();
|
||||
|
||||
foreach (var property in rowObject.Properties())
|
||||
{
|
||||
string columnName = property.Name;
|
||||
JToken value = property.Value;
|
||||
|
||||
if (newRow.ContainsKey(columnName) && ColumnsName.ContainsKey(columnName))
|
||||
{
|
||||
object control = newRow[columnName];
|
||||
|
||||
switch (ColumnsName[columnName])
|
||||
{
|
||||
case ColumnType.Button:
|
||||
if (control is Button button)
|
||||
button.Content = value.ToString();
|
||||
break;
|
||||
|
||||
case ColumnType.CheckBox:
|
||||
if (control is CheckBox checkBox)
|
||||
checkBox.IsChecked = (bool)value;
|
||||
break;
|
||||
|
||||
case ColumnType.ComboBox:
|
||||
if (control is ComboBox comboBox && value is JObject comboObj)
|
||||
{
|
||||
comboBox.SelectedValue = comboObj["SelectedValue"]?.ToString();
|
||||
comboBox.SelectedIndex = (int)comboObj["SelectedIndex"];
|
||||
// 注意:ItemsSource 需要额外处理,这里只恢复选中状态
|
||||
}
|
||||
break;
|
||||
|
||||
case ColumnType.TextBox:
|
||||
if (control is TextBox textBox && value is JObject textBoxObj)
|
||||
{
|
||||
textBox.Text = textBoxObj["Text"]?.ToString();
|
||||
if (textBoxObj["Background"] != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var converter = new BrushConverter();
|
||||
textBox.Background = (Brush)converter.ConvertFromString(textBoxObj["Background"].ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
textBox.Background = Brushes.Transparent;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ColumnType.Label:
|
||||
if (control is Label label && value is JObject labelObj)
|
||||
{
|
||||
label.Content = labelObj["Content"]?.ToString();
|
||||
if (labelObj["Background"] != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var converter = new BrushConverter();
|
||||
label.Background = (Brush)converter.ConvertFromString(labelObj["Background"].ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
label.Background = Brushes.Transparent;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新显示
|
||||
xDataGrid.Items.Refresh();
|
||||
OnPropertyChanged(nameof(Rows));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"加载JSON数据时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// 重载方法,支持直接从 JObject 加载
|
||||
public void JsonLoad(JObject jsonData)
|
||||
{
|
||||
JsonLoad(jsonData.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user