122 lines
4.5 KiB
C#
122 lines
4.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Collections.ObjectModel;
|
|||
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Controls;
|
|||
|
|
using System.Windows.Data;
|
|||
|
|
using static Ramitta.winDataGrid;
|
|||
|
|
|
|||
|
|
namespace Ramitta
|
|||
|
|
{
|
|||
|
|
public partial class winDataGrid : UserControl
|
|||
|
|
{
|
|||
|
|
public ObservableCollection<
|
|||
|
|
Dictionary<string, Dictionary<string, object>?>
|
|||
|
|
> Rows
|
|||
|
|
{ get; private set; }
|
|||
|
|
|
|||
|
|
public Dictionary<string,object> Columns=new();
|
|||
|
|
|
|||
|
|
public winDataGrid()
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
Rows = new();
|
|||
|
|
xDataGrid.ItemsSource = Rows;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 定义列类型的枚举
|
|||
|
|
public enum ColumnType
|
|||
|
|
{
|
|||
|
|
Button,
|
|||
|
|
CheckBox,
|
|||
|
|
ComboBox,
|
|||
|
|
TextBox,
|
|||
|
|
Label
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void AddColumn(string columnName, ColumnType columnType)
|
|||
|
|
{
|
|||
|
|
DataTemplate dataTemplate = new DataTemplate();
|
|||
|
|
DataGridTemplateColumn column = new DataGridTemplateColumn();
|
|||
|
|
FrameworkElementFactory? elementFactory=null;
|
|||
|
|
Columns.Add(columnName, columnType);
|
|||
|
|
switch (columnType)
|
|||
|
|
{
|
|||
|
|
case ColumnType.Button:
|
|||
|
|
elementFactory = new FrameworkElementFactory(typeof(Button));
|
|||
|
|
elementFactory.SetBinding(Button.ContentProperty,
|
|||
|
|
new Binding($"[{columnName}][Content]") { Mode = BindingMode.TwoWay });
|
|||
|
|
break;
|
|||
|
|
case ColumnType.CheckBox:
|
|||
|
|
elementFactory = new FrameworkElementFactory(typeof(CheckBox));
|
|||
|
|
elementFactory.SetValue(CheckBox.IsCheckedProperty,
|
|||
|
|
new Binding($"[{columnName}][IsChecked]") { Mode = BindingMode.TwoWay });
|
|||
|
|
break;
|
|||
|
|
case ColumnType.ComboBox:
|
|||
|
|
elementFactory = new FrameworkElementFactory(typeof(ComboBox));
|
|||
|
|
elementFactory.SetValue(ComboBox.SelectedIndexProperty,0);
|
|||
|
|
|
|||
|
|
elementFactory.SetValue(ComboBox.ItemsSourceProperty,
|
|||
|
|
new Binding($"[{columnName}][ItemsSource]") { Mode = BindingMode.TwoWay });
|
|||
|
|
|
|||
|
|
elementFactory.SetValue(ComboBox.SelectedValueProperty,
|
|||
|
|
new Binding($"[{columnName}][SelectedValue]") { Mode = BindingMode.TwoWay });
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
case ColumnType.Label:
|
|||
|
|
elementFactory = new FrameworkElementFactory(typeof(Label));
|
|||
|
|
elementFactory.SetBinding(Label.ContentProperty,
|
|||
|
|
new Binding($"[{columnName}][Content]") { Mode = BindingMode.TwoWay });
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
case ColumnType.TextBox:
|
|||
|
|
elementFactory = new FrameworkElementFactory(typeof(TextBox));
|
|||
|
|
elementFactory.SetBinding(TextBox.TextProperty,
|
|||
|
|
new Binding($"[{columnName}][Text]") { Mode = BindingMode.TwoWay });
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
dataTemplate.VisualTree = elementFactory;
|
|||
|
|
column.Header = columnName;
|
|||
|
|
column.CellTemplate = dataTemplate;
|
|||
|
|
column.CellEditingTemplate = dataTemplate;
|
|||
|
|
xDataGrid.Columns.Add( column );
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public Dictionary<string, Dictionary<string, object>?> AddRow(){
|
|||
|
|
var keys = Columns.Keys.ToList();
|
|||
|
|
var row = new Dictionary<string, Dictionary<string, object>?> { };
|
|||
|
|
|
|||
|
|
foreach (var key in keys) {
|
|||
|
|
var value = new Dictionary<string, object?> { };
|
|||
|
|
switch (Columns[key])
|
|||
|
|
{
|
|||
|
|
case ColumnType.Button:
|
|||
|
|
value.Add("Content","value");
|
|||
|
|
break;
|
|||
|
|
case ColumnType.CheckBox:
|
|||
|
|
value.Add("IsChecked", false);
|
|||
|
|
break;
|
|||
|
|
case ColumnType.ComboBox:
|
|||
|
|
value.Add("ItemsSource", null);
|
|||
|
|
value.Add("SelectedValue", null);
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
case ColumnType.TextBox:
|
|||
|
|
value.Add("Text", "value");
|
|||
|
|
break;
|
|||
|
|
case ColumnType.Label:
|
|||
|
|
value.Add("Content", "value");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
row.Add(key, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Rows.Add(row);
|
|||
|
|
return row;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|