From a163c4a3cb0ef47461a06fb10077966d298dc2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=AF=E5=8B=87?= <柯勇> Date: Thu, 18 Mar 2021 09:43:07 +0800 Subject: [PATCH] =?UTF-8?q?Cascader=20Text=E6=95=B0=E6=8D=AE=E7=BB=91?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SmartUI.Demo/MainWindow.xaml | 2 +- SmartUI/Controls/Cascader.cs | 88 ++++++++++++++++++++++++++++-------- 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/SmartUI.Demo/MainWindow.xaml b/SmartUI.Demo/MainWindow.xaml index b5565d3..a35db0a 100644 --- a/SmartUI.Demo/MainWindow.xaml +++ b/SmartUI.Demo/MainWindow.xaml @@ -130,7 +130,7 @@ - + diff --git a/SmartUI/Controls/Cascader.cs b/SmartUI/Controls/Cascader.cs index bf43358..9be69d4 100644 --- a/SmartUI/Controls/Cascader.cs +++ b/SmartUI/Controls/Cascader.cs @@ -32,16 +32,62 @@ namespace SmartUI.Controls return; } cascader.RaiseItems(source); + if (!string.IsNullOrEmpty(cascader.Text)) + cascader.ResetByText(); } + public new string Text + { + get { return (string)GetValue(TextProperty); } + set { SetValue(TextProperty, value); } + } + public static new readonly DependencyProperty TextProperty = + DependencyProperty.Register(nameof(Text), typeof(string), typeof(Cascader), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, TextChanged)); + + private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + Cascader control = d as Cascader; + if (control.IsDropDownOpen) + return; + control.ResetByText(); + } - protected override void OnSelectionChanged(SelectionChangedEventArgs e) + private void ResetByText() { - base.OnSelectionChanged(e); - IsDropDownOpen = true; + string text = Text; + int index = 0; + if (ItemsSource?.Count > 0 && !string.IsNullOrWhiteSpace(text)) + { + ObservableCollection items = ItemsSource; + CascaderItem selectItem = null; + do + { + foreach (var item in items) + { + item.ParentTree = selectItem; + if (text.StartsWith(item.DisplayName)) + { + item.IsChecked = true; + text = text.Substring(item.DisplayName.Length).TrimStart('/'); + break; + } + } + selectItem = items.FirstOrDefault(p => p.IsChecked); + if (_itemsPanle.Children.Count <= index) + { + RaiseItems(items); + } + else + { + (_itemsPanle.Children[index] as ListBox).ItemsSource = items; + } + index++; + items = selectItem?.Children; + } while (items != null); + } } public override void OnApplyTemplate() @@ -52,6 +98,8 @@ namespace SmartUI.Controls RaiseItems(ItemsSource); _border = GetTemplateChild("border") as Border; _border.MouseLeftButtonDown += Border_MouseLeftButtonDown; + if (!string.IsNullOrEmpty(Text)) + ResetByText(); } private void RaiseItems(ObservableCollection source) @@ -76,27 +124,29 @@ namespace SmartUI.Controls model.IsChecked = true; if (model.Children is null || model.Children.Count == 0) { - IsDropDownOpen = false; if (index + 1 < _itemsPanle.Children.Count) _itemsPanle.Children.RemoveRange(index + 1, _itemsPanle.Children.Count - index - 1); this.Text = GetParentNames(model); - this.SelectedItem = model.Data; - return; - } - IsDropDownOpen = true; - foreach (var item in model.Children) - { - item.ParentTree = model; - } - if (_itemsPanle.Children.Count == index + 1) - { - RaiseItems(model.Children); + this.SelectedItem = model; + IsDropDownOpen = false; } - else if (_itemsPanle.Children.Count - 1 > index) + else { - (_itemsPanle.Children[index + 1] as ListBox).ItemsSource = model.Children; - if (index + 2 < _itemsPanle.Children.Count) - _itemsPanle.Children.RemoveRange(index + 2, _itemsPanle.Children.Count - index - 2); + IsDropDownOpen = true; + foreach (var item in model.Children) + { + item.ParentTree = model; + } + if (_itemsPanle.Children.Count == index + 1) + { + RaiseItems(model.Children); + } + else if (_itemsPanle.Children.Count - 1 > index) + { + (_itemsPanle.Children[index + 1] as ListBox).ItemsSource = model.Children; + if (index + 2 < _itemsPanle.Children.Count) + _itemsPanle.Children.RemoveRange(index + 2, _itemsPanle.Children.Count - index - 2); + } } if (_itemsPanle.Children.Count > 0) (_itemsPanle.Children[_itemsPanle.Children.Count - 1] as ListBox).BorderThickness = new Thickness(0); -- GitLab