using IStation.Untity;
|
using System;
|
using System.ComponentModel;
|
using System.Linq;
|
|
namespace IStation.WinFrmUI.Basic
|
{
|
public partial class SignalTypeTreeListLookUpEdit : DevExpress.XtraEditors.XtraUserControl
|
{
|
public SignalTypeTreeListLookUpEdit()
|
{
|
InitializeComponent();
|
this.treeListLookUpEdit1.EditValueChanged += treeListLookUpEdit1_EditValueChanged;
|
this.treeListLookUpEdit1TreeList.InitialDefaultSettings();
|
this.treeListLookUpEdit1TreeList.SelectImageList = ImageLib.Lib;
|
this.treeListLookUpEdit1TreeList.OptionsView.ShowIndicator = false;
|
this.treeListLookUpEdit1.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
|
}
|
|
|
public class CurrentViewModel
|
{
|
public CurrentViewModel() { }
|
public CurrentViewModel(Model.SignalTypeGroup rhs)
|
{
|
this.ID = rhs.ID;
|
this.Name = rhs.Name;
|
this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
|
this.Identifier = string.Empty;
|
this.ImageIndex = ImageLib.Group;
|
this.IsGroup = true;
|
this.Model = rhs;
|
}
|
|
public CurrentViewModel(Model.SignalType rhs)
|
{
|
this.ID = rhs.ID;
|
this.Name = rhs.Name;
|
this.ParentID = rhs.GroupID;
|
this.Identifier = rhs.Identifier;
|
this.ImageIndex = ImageLib.SignalType;
|
this.IsGroup = false;
|
this.Model = rhs;
|
}
|
|
public long ID { get; set; }
|
public string Name { get; set; }
|
public string Identifier { get; set; }
|
public long ParentID { get; set; }
|
public int ImageIndex { get; set; }
|
public bool IsGroup { get; set; }
|
public object Model { get; set; }
|
}
|
/// <summary>
|
/// 选择项
|
/// </summary>
|
public Model.SignalType Selected { get => _selected; }
|
private Model.SignalType _selected { get; set; }
|
|
/// <summary>
|
/// 选择项变换
|
/// </summary>
|
public event Action<Model.SignalType> SelectedChangedEvent;
|
private BindingList<CurrentViewModel> _allBindingList { get; set; }
|
|
|
/// <summary>
|
/// 绑定数据
|
/// </summary>
|
public void SetBindingData(string identifier = null)
|
{
|
_allBindingList = new BindingList<CurrentViewModel>();
|
var signalTypeGroups = new BLL.SignalTypeGroup().GetAll();
|
if (signalTypeGroups != null && signalTypeGroups.Count > 0)
|
{
|
foreach (var item in signalTypeGroups)
|
{
|
var vm = new CurrentViewModel(item);
|
_allBindingList.Add(vm);
|
}
|
}
|
|
var signalTypes = new BLL.SignalType().GetAll();
|
if (signalTypes != null && signalTypes.Count > 0)
|
{
|
foreach (var item in signalTypes)
|
{
|
var vm = new CurrentViewModel(item);
|
_allBindingList.Add(vm);
|
}
|
}
|
|
this.currentViewModelBindingSource.DataSource = _allBindingList;
|
|
|
if (signalTypes != null && signalTypes.Any())
|
{
|
if (identifier == null)
|
{
|
identifier = signalTypes.First().Identifier;
|
}
|
var vm = _allBindingList.Where(x => x.Identifier == identifier).FirstOrDefault();
|
if (vm != null)
|
{
|
this.treeListLookUpEdit1.EditValue = vm.ID;
|
SelectedChanged(vm);
|
}
|
}
|
|
this.treeListLookUpEdit1TreeList.ExpandAll();
|
}
|
|
|
//选择对象变换
|
private void SelectedChanged(CurrentViewModel vm)
|
{
|
_selected = vm?.Model as Model.SignalType;
|
if (SelectedChangedEvent != null)
|
{
|
this.SelectedChangedEvent(_selected);
|
}
|
}
|
|
//选中前
|
private void treeListLookUpEdit1TreeList_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e)
|
{
|
var vm = this.treeListLookUpEdit1TreeList.GetDataRecordByNode(e.Node) as CurrentViewModel;
|
if (vm != null)
|
{
|
e.CanFocus = !vm.IsGroup;
|
}
|
}
|
|
//值变换
|
private void treeListLookUpEdit1_EditValueChanged(object sender, EventArgs e)
|
{
|
var vm = this.treeListLookUpEdit1TreeList.GetCurrentViewModel(_allBindingList);
|
if (vm != null)
|
{
|
SelectedChanged(vm);
|
}
|
}
|
|
|
}
|
}
|