68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace WinFormsLibraryVolkov
|
|
{
|
|
public partial class CustomTreeCell : UserControl
|
|
{
|
|
public CustomTreeCell()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
treeView.Nodes.Clear();
|
|
}
|
|
|
|
//Публичное свойство для установки и получения индекса выбранной ветки(set, get).
|
|
protected DataTreeNodeConfig Levels { get; set; }
|
|
|
|
public void LoadConfig(DataTreeNodeConfig levels)
|
|
{
|
|
if (levels != null)
|
|
{
|
|
Levels = levels;
|
|
}
|
|
}
|
|
|
|
public T GetSelectedObject<T>() where T : class, new()
|
|
{
|
|
if (treeView.SelectedNode == null || Levels == null || treeView.SelectedNode.Nodes.Count > 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Stack<string> stack = new Stack<string>();
|
|
for (TreeNode treeNode = treeView.SelectedNode; treeNode != null; treeNode = treeNode.Parent)
|
|
{
|
|
stack.Push(treeNode.Text);
|
|
}
|
|
|
|
if (stack.Count != Levels.NodeNames.Count)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
T val = new T();
|
|
foreach (string nodeName in Levels.NodeNames)
|
|
{
|
|
PropertyInfo property = val.GetType().GetProperty(nodeName);
|
|
string value = stack.Pop();
|
|
property?.SetValue(val, Convert.ChangeType(value, property?.PropertyType));
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
}
|
|
}
|