private void CreateNextContainer(IList selectedItem, int deep) { if (selectedItem.Count <= 0) { return; } object obj = selectedItem[0]; Type type = obj.GetType(); System.Reflection.PropertyInfo propertyInfo = type.GetProperty(this.ChildMemberPath); IList list = (IList)propertyInfo.GetValue(obj, null); //获取属性值 if (list != null) { if (this.ListBoxContainer.Count > deep) { CascaderListBox listBox = this.ListBoxContainer[deep]; listBox.SetValue(CascaderListBox.VisibilityProperty, Visibility.Visible); listBox.SetValue(CascaderListBox.ItemsSourceProperty, list); listBox.SetValue(CascaderListBox.ParentItemProperty, selectedItem); for (int i = deep + 1; i < this.ListBoxContainer.Count; i++) { this.ListBoxContainer[i].Visibility = Visibility.Collapsed; } } else { this.CreateContainer(list, deep, obj); } } else { if (this.ListBoxContainer.Count > deep) { for (int i = deep; i < this.ListBoxContainer.Count; i++) { this.ListBoxContainer[i].Visibility = Visibility.Collapsed; } } } }
private void CreateContainer(object itemsSource, int deep, object parent) { CascaderListBox container = new CascaderListBox(); container.Owner = this; container.SetValue(CascaderListBox.ParentItemProperty, parent); container.SetValue(CascaderListBox.ItemsSourceProperty, itemsSource); container.SetValue(CascaderListBox.DisplayMemberPathProperty, this.DisplayMemberPath); container.SetValue(CascaderListBox.DeepProperty, deep); container.SetValue(CascaderListBox.StyleProperty, this.ValueBoxStyle); container.SetValue(CascaderListBox.ItemContainerStyleProperty, this.ValueItemStyle); container.SelectionChanged += Container_SelectionChanged; container.ItemClick += Container_ItemClick; this.ListBoxContainer.Add(container); }