DevExpress WPF界面开发——如何在LookUpEdit弹出窗口中显示TreeListView
通过DevExpress WPF Controls,您能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。
遇到的问题
有以下DTO类(在WCF服务中):
/// <summary> /// Класс ТБК /// </summary> [DataContract] public class DTOTbkInfo { /// <summary> /// Код ТБК /// </summary> [DataMember] public int Code { get; set; } /// <summary> /// Название ТБК /// </summary> [DataMember] public string Name { get; set; } /// <summary> /// Код уровня ТБК /// </summary> [DataMember] public int LevelCode { get; set; } /// <summary> /// Код родительского ТБК /// </summary> [DataMember] public int? ParentCode { get => ParentTbk?.Code; set { } } /// <summary> /// Родительское ТБК /// </summary> public DTOTbkInfo ParentTbk { get; set; } /// <summary> /// Список дочерних ТБК /// </summary> [DataMember] public List<DTOTbkInfo> ChildTbkList { get; set; } public DTOTbkInfo() { //empty code } public DTOTbkInfo(GetTbkList_Result tbk) { Code = tbk.tbk; Name = tbk.name; LevelCode = tbk.level; } public static DTOTbkInfo Create(GetTbkList_Result tbk) => new DTOTbkInfo(tbk); }
在此类中,ChildTbkList属性包含子类列表,并且ParentTbk属性对客户端不可用(出于服务的内部目的),ParentCode属性包含父记录的代码。
在客户端上,使用以下数据模型:
/// <summary> /// Класс с информацией о ТБК товара /// </summary> public class TbkInfo : ViewModelBase { /// <summary> /// Код ТБК /// </summary> public int Code { get; set; } /// <summary> /// Название ТБК /// </summary> public string Name { get; set; } /// <summary> /// Общая информация о ТБК /// </summary> public string DisplayName => $"ТБК: {Code} \"{Name}\""; public TbkInfo() { //empty code } public TbkInfo(int code, string name) { Code = code; Name = name; } public static TbkInfo Create(int code, string name) => new TbkInfo(code, name); }
和类:
/// <summary> /// Класс с дополнительной информацией о ТБК товара /// </summary> public class TbkInfoExt : TbkInfo { /// <summary> /// Номер уровня /// </summary> public int LevelCode { get; set; } /// <summary> /// Код родительского ТБК /// </summary> public int? ParentCode { get; set; } /// <summary> /// Список дочерних ТБК /// </summary> public List<TbkInfoExt> ChildTbkList { get; set; } private ObservableCollection<DTOCatalogPropertyForTbk> _catalogPropertyList; /// <summary> /// Список свойств товара /// </summary> public ObservableCollection<DTOCatalogPropertyForTbk> CatalogPropertyList { get => _catalogPropertyList; set { _catalogPropertyList = value; OnPropertyChanged(nameof(CatalogPropertyList)); } } public TbkInfoExt(DTOTbkInfo tbkInfo) : base(tbkInfo.Code, tbkInfo.Name) { LevelCode = tbkInfo.LevelCode; ParentCode = tbkInfo.ParentCode; tbkInfo.ChildTbkList?.ForEach(p => { if (ChildTbkList == null) { ChildTbkList = new List<TbkInfoExt>(); } ChildTbkList.Add(TbkInfoExt.Create(p)); }); } public static TbkInfoExt Create(DTOTbkInfo tbkInfo) => new TbkInfoExt(tbkInfo); }
有以下视图模型:
public class EditRelationPropertyTbkModel : ViewModelBase { private readonly IApplicationService _appService; #region Binding properties private ObservableCollection<TbkInfoExt> _tbkList; /// <summary> /// Список ТБК /// </summary> public ObservableCollection<TbkInfoExt> TbkList { get => _tbkList; set { _tbkList = value; OnPropertyChanged(nameof(TbkList)); } } private TbkInfoExt _currentTbk; /// <summary> /// Текущий ТБК /// </summary> public TbkInfoExt CurrentTbk { get => _currentTbk; set { _currentTbk = value; OnPropertyChanged(nameof(CurrentTbk)); DefinitionCatalogPropertyList(); } } private ObservableCollection<DTOCatalogPropertyForTbk> _catalogPropertyList; /// <summary> /// Список свойств /// </summary> public ObservableCollection<DTOCatalogPropertyForTbk> CatalogPropertyList { get => _catalogPropertyList; set { _catalogPropertyList = value; OnPropertyChanged(nameof(CatalogPropertyList)); } } #endregion #region Commands private RelayCommand _saveCommand; public RelayCommand SaveCommand => _saveCommand ?? (_saveCommand = new RelayCommand(obj => { }, e => true)); private RelayCommand _cancelCommand; public RelayCommand CancelCommand => _cancelCommand ?? (_cancelCommand = new RelayCommand(obj => { })); #endregion #region Methods /// <summary> /// Метод определения списка свойств /// </summary> private void DefinitionCatalogPropertyList() { using (var proxy = new ProxyCatalog()) { var results = proxy.GetPropertyListForTbk(CurrentTbk.Code, true); CatalogPropertyList = new ObservableCollection<DTOCatalogPropertyForTbk>(results); } } #endregion public EditRelationPropertyTbkModel() { //empty code } public EditRelationPropertyTbkModel(IApplicationService appService) { _appService = appService; }
有以下视图(部分代码):
<dxg:LookUpEdit ItemsSource="{Binding TbkList}" SelectedItem="{Binding CurrentTbk}" DisplayMember="Name"> <dxg:LookUpEdit.StyleSettings> <dxg:SearchLookUpEditStyleSettings/> </dxg:LookUpEdit.StyleSettings> <dxg:LookUpEdit.PopupContentTemplate> <ControlTemplate> <dxg:GridControl x:Name="PART_GridControl"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Code" Header="Код ТБК" /> <dxg:GridColumn FieldName="Name" Header="Название ТБК"/> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TreeListView AutoWidth="True" KeyFieldName="Code" TreeDerivationMode="ChildNodesSelector" ChildNodesPath="ChildTbkList" AllowEditing="False"/> </dxg:GridControl.View> </dxg:GridControl> </ControlTemplate> </dxg:LookUpEdit.PopupContentTemplate> </dxg:LookUpEdit>
问题:为什么不能选择根元素的子元素(在CurrentTbk属性ViewModel的SET中,不执行任何转换),而根元素本身也可以(在CurrentTbk属性ViewModel的SET中进行过渡) 执行。
解决方案:
LookUpEdit和ComboBoxEdit编辑器只能选择其ItemsSource集合中存在的那些值。在这种情况下,此集合中不包含子项,因此就是发生此问题的原因。
作为解决方案,可以将LookUpEdit.ItemsSource设置为包含父项和子项的集合,然后将GridControl.ItemsSource绑定到原始的TbkList集合。
DevExpress技术交流群2:775869749 欢迎一起进群讨论