using DevExpress.Utils.Extensions;
using System.ComponentModel;
using Yw.Dto;
namespace HStation.WinFrmUI.Basic
{
public partial class SysTypeTreeListLookUpEdit : DevExpress.XtraEditors.XtraUserControl
{
public SysTypeTreeListLookUpEdit()
{
InitializeComponent();
this.treeListLookUpEdit1.EditValueChanged += treeListLookUpEdit1_EditValueChanged;
this.treeListLookUpEdit1TreeList.InitialDefaultSettings();
this.treeListLookUpEdit1TreeList.OptionsView.ShowIndicator = false;
this.treeListLookUpEdit1.Properties.PopupFormSize = new System.Drawing.Size(200, 400);
}
public class CurrentViewModel
{
public CurrentViewModel(Yw.Vmo.SysTypeLogicalTreeVmo rhs)
{
var module = rhs.LogicalModel as Yw.Dto.SysModuleStdDto;
this.ID = module.ID;
this.Name = module.Name;
this.SortCode = module.SortCode;
this.Description = module.Description;
}
public CurrentViewModel(SysTypeStdDto rhs)
{
this.ID = rhs.ID;
this.Name = rhs.Name;
this.SortCode = rhs.SortCode;
this.Description = rhs.Description;
this.ParentID = rhs.ModuleID;
}
///
/// ID
///
public long ID { get; set; }
///
/// 父级ID
///
public long ParentID { get; set; }
///
/// 名称
///
public string Name { get; set; }
///
/// 排序码
///
public int SortCode { get; set; }
///
/// 说明
///
public string Description { get; set; }
}
private Yw.BLL.SysTypeStd _bll = null;
///
/// 选择项变换
///
public event Action SelectedChangedEvent;
private BindingList _allBindingList { get; set; }
///
/// 绑定数据
///
public async void SetBindingData()
{
_bll = new Yw.BLL.SysTypeStd();
var alllist = await _bll.GetExtendLogicalTreeList();
if (alllist == null)
return;
_allBindingList = new BindingList();
foreach (var item in alllist)
{
_allBindingList.Add(new CurrentViewModel(item));
foreach (var child in item.Children)
{
var type = child.LogicalModel as Yw.Dto.SysTypeStdDto;
if (type != null)
{
_allBindingList.Add(new CurrentViewModel(type));
}
}
}
this.treeListLookUpEdit1TreeList.DataSource = _allBindingList;
this.treeListLookUpEdit1TreeList.ExpandAll();
if (_allBindingList.Count > 0)
{
var vm = _allBindingList.Where(x => x.ParentID != 0).First();
this.treeListLookUpEdit1.EditValue = vm.ID;
SelectedChanged(vm);
}
}
//选择对象变换
private void SelectedChanged(CurrentViewModel vm)
{
if (SelectedChangedEvent != null)
{
SelectedChangedEvent.Invoke(vm.ID);
}
}
//值变换
private void treeListLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
var vm = this.treeListLookUpEdit1TreeList.GetCurrentViewModel(_allBindingList);
if (vm != null)
{
SelectedChanged(vm);
}
}
public long GetCurrentTypeID()
{
var vm = this.treeListLookUpEdit1TreeList.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return default;
}
return vm.ID;
}
}
}