經過DevExpress WPF Controls,您能建立有着強大互動功能的XAML基礎應用程序,這些應用程序專一於當代客戶的需求和構建將來新一代支持觸摸的解決方案。express
遇到的問題app
有如下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屬性包含父記錄的代碼。ui
在客戶端上,使用如下數據模型:3d
/// <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); }
和類:code
/// <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); }
有如下視圖模型:htm
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; }
有如下視圖(部分代碼):blog
<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中進行過渡) 執行。ip
解決方案:
LookUpEdit和ComboBoxEdit編輯器只能選擇其ItemsSource集合中存在的那些值。在這種狀況下,此集合中不包含子項,所以就是發生此問題的緣由。
做爲解決方案,能夠將LookUpEdit.ItemsSource設置爲包含父項和子項的集合,而後將GridControl.ItemsSource綁定到原始的TbkList集合。
DevExpress技術交流羣2:775869749 歡迎一塊兒進羣討論