C# PropertyGrid组件基本使用
Demo源码
C# 全选
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace PropertyGridDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//对象实例
var node = new FlowNode
{
NodeId = "20230001",
NodeName = "流程节点",
Height = 100,
Width = 200,
X = 423,
Y = 123,
};
//给组件设置实例对象
propertyGrid1.SelectedObject = node;
//表格样式设置
this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.DataSource = new List<FlowNode>() { node };
this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
}
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
var item = e.ChangedItem;//当前属性项
var oldValue = e.OldValue;//旧值
var newValue = item.Value;//新值
var propertyName = item.Label;//标签,属性名称
//表格定位焦点列
var col = dataGridView1.Columns[propertyName];
dataGridView1.CurrentCell = dataGridView1[col.Index, 0]; //设置当前单元格
dataGridView1.Refresh();
}
private void propertyGrid1_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
var col = dataGridView1.Columns[e.NewSelection.Label];
if (col != null)
{
dataGridView1.CurrentCell = dataGridView1[col.Index, 0]; //设置当前单元格
}
}
}
}
FlowNode 对象模型
C# 全选
public class FlowNode
{
[Description("节点编号"), CategoryAttribute("节点"), Browsable(false)]
public string NodeId { get; set; }
[Description("节点名称"), CategoryAttribute("节点")]
public string NodeName { get; set; }
[Description("节点宽度")]
public Int32 Width { get; set; }
[Description("节点高度")]
public Int32 Height { get; set; }
[CategoryAttribute("坐标")]
[Description("X坐标")]
public Int32 X { get; set; }
[CategoryAttribute("坐标")]
[Description("Y坐标")]
public Int32 Y { get; set; }
[Description("字体颜色")]
public Color FontColor { get; set; }
[Description("背景色")]
public Color BackColor { get; set; }
[Description("字体")]
public Font Font { get; set; }
}
测试
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网