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); }
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 Container_ItemClick(object sender, RoutedPropertyChangedEventArgs <object> e) { this.SelectedValues.Clear(); CascaderListBox cascaderListBox = sender as CascaderListBox; if (!this.HasChildren(e.NewValue)) { this.ShowText = string.Empty; for (int i = 0; i < cascaderListBox.Deep + 1; i++) { CascaderListBox listBox = this.ListBoxContainer[i] as CascaderListBox; if (listBox.Visibility == Visibility.Visible) { this.ShowText = this.ShowText + this.GetPropertyValue(listBox.SelectedItem) + this.Separator; this.SelectedValues.Add(listBox.SelectedItem); } } } this.PART_TextBox.Text = this.ShowText.TrimEnd(this.Separator.ToCharArray()); }
private void Container_SelectionChanged(object sender, SelectionChangedEventArgs e) { CascaderListBox listBox = sender as CascaderListBox; this.CreateNextContainer(e.AddedItems, listBox.Deep + 1); }