表格基本接口完成
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
@@ -8,14 +9,16 @@ using static Ramitta.winDataGrid;
|
||||
|
||||
namespace Ramitta
|
||||
{
|
||||
public partial class winDataGrid : UserControl
|
||||
public partial class winDataGrid : UserControl, INotifyPropertyChanged
|
||||
{
|
||||
public DataGrid DataGridControl => xDataGrid;
|
||||
|
||||
public ObservableCollection<
|
||||
Dictionary<string, Dictionary<string, object>?>
|
||||
Dictionary<string, object>
|
||||
> Rows
|
||||
{ get; private set; }
|
||||
|
||||
public Dictionary<string,object> Columns=new();
|
||||
public Dictionary<string,object> ColumnsName=new();
|
||||
|
||||
public winDataGrid()
|
||||
{
|
||||
@@ -34,88 +37,121 @@ namespace Ramitta
|
||||
Label
|
||||
}
|
||||
|
||||
public void InitColumns(params (string columnName, ColumnType columnType)[] columns)
|
||||
{
|
||||
// 循环添加列
|
||||
foreach (var column in columns)
|
||||
{
|
||||
AddColumn(column.columnName, column.columnType);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetBindingToProperty(FrameworkElementFactory elementFactory, DependencyProperty property, string path)
|
||||
{
|
||||
elementFactory.SetValue(property,
|
||||
new Binding(path)
|
||||
{
|
||||
Mode = BindingMode.TwoWay,
|
||||
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
|
||||
});
|
||||
}
|
||||
|
||||
public void AddColumn(string columnName, ColumnType columnType)
|
||||
{
|
||||
DataTemplate dataTemplate = new DataTemplate();
|
||||
DataGridTemplateColumn column = new DataGridTemplateColumn();
|
||||
FrameworkElementFactory? elementFactory=null;
|
||||
Columns.Add(columnName, columnType);
|
||||
ColumnsName.Add(columnName, columnType);
|
||||
|
||||
switch (columnType)
|
||||
{
|
||||
case ColumnType.Button:
|
||||
elementFactory = new FrameworkElementFactory(typeof(Button));
|
||||
elementFactory.SetBinding(Button.ContentProperty,
|
||||
new Binding($"[{columnName}][Content]") { Mode = BindingMode.TwoWay });
|
||||
SetBindingToProperty(elementFactory, Button.ContentProperty, $"[{columnName}].Content");
|
||||
break;
|
||||
case ColumnType.CheckBox:
|
||||
elementFactory = new FrameworkElementFactory(typeof(CheckBox));
|
||||
elementFactory.SetValue(CheckBox.IsCheckedProperty,
|
||||
new Binding($"[{columnName}][IsChecked]") { Mode = BindingMode.TwoWay });
|
||||
SetBindingToProperty(elementFactory, CheckBox.IsCheckedProperty, $"[{columnName}].IsChecked");
|
||||
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 });
|
||||
|
||||
elementFactory.SetValue(ComboBox.SelectedIndexProperty, 0);
|
||||
SetBindingToProperty(elementFactory, ComboBox.ItemsSourceProperty, $"[{columnName}].ItemsSource");
|
||||
SetBindingToProperty(elementFactory, ComboBox.SelectedValueProperty, $"[{columnName}].SelectedValue");
|
||||
break;
|
||||
case ColumnType.Label:
|
||||
elementFactory = new FrameworkElementFactory(typeof(Label));
|
||||
elementFactory.SetBinding(Label.ContentProperty,
|
||||
new Binding($"[{columnName}][Content]") { Mode = BindingMode.TwoWay });
|
||||
|
||||
SetBindingToProperty(elementFactory, Label.ContentProperty, $"[{columnName}].Content");
|
||||
SetBindingToProperty(elementFactory, Label.BackgroundProperty, $"[{columnName}].Background");
|
||||
break;
|
||||
case ColumnType.TextBox:
|
||||
elementFactory = new FrameworkElementFactory(typeof(TextBox));
|
||||
elementFactory.SetBinding(TextBox.TextProperty,
|
||||
new Binding($"[{columnName}][Text]") { Mode = BindingMode.TwoWay });
|
||||
|
||||
SetBindingToProperty(elementFactory, TextBox.TextProperty, $"[{columnName}].Text");
|
||||
SetBindingToProperty(elementFactory, TextBox.BackgroundProperty, $"[{columnName}].Background");
|
||||
break;
|
||||
}
|
||||
dataTemplate.VisualTree = elementFactory;
|
||||
DataTemplate dataTemplate = new 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>?> { };
|
||||
|
||||
|
||||
public Dictionary<string, object> AddRow(){
|
||||
var keys = ColumnsName.Keys.ToList();
|
||||
var row = new Dictionary<string, object> { };
|
||||
|
||||
foreach (var key in keys) {
|
||||
var value = new Dictionary<string, object?> { };
|
||||
switch (Columns[key])
|
||||
|
||||
switch (ColumnsName[key])
|
||||
{
|
||||
case ColumnType.Button:
|
||||
value.Add("Content","value");
|
||||
row.Add(key, new Button());
|
||||
break;
|
||||
case ColumnType.CheckBox:
|
||||
value.Add("IsChecked", false);
|
||||
row.Add(key, new CheckBox());
|
||||
break;
|
||||
case ColumnType.ComboBox:
|
||||
value.Add("ItemsSource", null);
|
||||
value.Add("SelectedValue", null);
|
||||
|
||||
row.Add(key, new ComboBox());
|
||||
break;
|
||||
case ColumnType.TextBox:
|
||||
value.Add("Text", "value");
|
||||
row.Add(key, new TextBox());
|
||||
break;
|
||||
case ColumnType.Label:
|
||||
value.Add("Content", "value");
|
||||
row.Add(key, new Label());
|
||||
break;
|
||||
}
|
||||
row.Add(key, value);
|
||||
}
|
||||
|
||||
Rows.Add(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
Rows.Clear();
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return Rows.Count();
|
||||
}
|
||||
|
||||
public void DeleteRow(Dictionary<string, object> row)
|
||||
{
|
||||
// 从 Rows 集合中移除指定的行
|
||||
if (Rows.Contains(row))
|
||||
{
|
||||
Rows.Remove(row);
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user