DevExpress WPF使用技巧教程:如何在LookUpEdit的彈出窗口中顯示TreeListView
下載DevExpress v20.1完整版 DevExpress v20.1漢化資源獲取
通過DevExpress WPF Controls,您能創(chuàng)建有著強(qiáng)大互動功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。
遇到的問題
有以下DTO類(在WCF服務(wù)中):
/// <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屬性對客戶端不可用(出于服務(wù)的內(nèi)部目的),ParentCode屬性包含父記錄的代碼。
在客戶端上,使用以下數(shù)據(jù)模型:
/// <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中,不執(zhí)行任何轉(zhuǎn)換),而根元素本身也可以(在CurrentTbk屬性ViewModel的SET中進(jìn)行過渡) 執(zhí)行。
解決方案:
LookUpEdit和ComboBoxEdit編輯器只能選擇其ItemsSource集合中存在的那些值。在這種情況下,此集合中不包含子項(xiàng),因此就是發(fā)生此問題的原因。
作為解決方案,可以將LookUpEdit.ItemsSource設(shè)置為包含父項(xiàng)和子項(xiàng)的集合,然后將GridControl.ItemsSource綁定到原始的TbkList集合。
DevExpress技術(shù)交流群2:775869749 歡迎一起進(jìn)群討論