using DevExpress.XtraEditors;
using System;
using System.ComponentModel;
using System.Linq;
namespace IStation.WinFrmUI.Basic
{
///
///
///
public partial class PumpListCtrl : XtraUserControl
{
public PumpListCtrl()
{
InitializeComponent();
this.treeList1.InitialDefaultSettings();
this.treeList1.SelectImageList = ImageLib.Lib;
this.layoutControl1.SetupLayoutControl();
}
public class CurrentViewModel
{
public long ID { get; set; }
public long ParentID { get; set; }
public string Name { get; set; }
public string ObjectType { get; set; }
public object Model { get; set; }
public bool IsGroup { get; set; }
public int ImageIndex { get; set; }
}
///
/// 聚焦改变事件
///
public event Action> FocusedChangedEvent;
private BindingList _allBindingList = null;//所有绑定列表
///
/// 绑定数据
///
public void Clear()
{
_allBindingList = new BindingList();
this.treeList1.DataSource = _allBindingList;
this.FocusedChangedEvent?.Invoke(null);
}
///
/// 绑定数据
///
public void SetBindingData()
{
WaitFrmHelper.ShowWaitForm("正在加载数据...");
_allBindingList = new BindingList();
var stations = new BLL.Station().GetAll();
if (stations != null && stations.Any())
{
var imgIndex = ImageLib.Station;
foreach (var station in stations)
{
var vm = new CurrentViewModel()
{
ID = station.ID,
Name = station.Name,
ObjectType = IStation.ObjectType.Station,
Model = station,
IsGroup = true,
ImageIndex = imgIndex
};
_allBindingList.Add(vm);
}
}
long? key = null;
var pumps = new BLL.Product().GetAllPump();
if (pumps != null && pumps.Any())
{
var first = pumps.First();
key = first.ID;
var imgIndex = ImageLib.Pump;
foreach (var pump in pumps)
{
var vm = new CurrentViewModel()
{
ID = pump.ID,
Name = pump.Name,
ParentID = pump.BelongID,
ObjectType = IStation.ObjectType.Product,
Model = pump,
IsGroup = false,
ImageIndex = imgIndex
};
_allBindingList.Add(vm);
}
}
this.treeList1.DataSource = _allBindingList;
this.treeList1.ForceInitialize();
if (key.HasValue)
{
this.treeList1.FocusedNode = this.treeList1.FindNodeByKeyID(key.Value);
}
WaitFrmHelper.HideWaitForm();
}
//全部展开
private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.ExpandAll();
}
//全部折叠
private void barBtnCollpseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.CollapseAll();
}
//检索
private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (this.barCkSearch.Checked)
this.treeList1.ShowFindPanel();
else
this.treeList1.HideFindPanel();
}
//详细信息
private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
XtraMessageBox.Show("待补充!");
}
//聚焦前
private void treeList1_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e)
{
var row = this.treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
if (row != null)
{
if (row.IsGroup)
{
e.CanFocus = false;
}
}
}
//聚焦改变
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
return;
if (vm.IsGroup)
return;
var model = vm.Model as Model.Product;
this.FocusedChangedEvent?.Invoke(model);
}
}
}