using PDF_Office.CustomControl; using PDF_Office.Model.BOTA; using PDF_Office.ViewModels.BOTA; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PDF_Office.Views.BOTA { /// /// SearchContent.xaml 的交互逻辑 /// public partial class SearchContent : UserControl { public SearchContent() { InitializeComponent(); } List HistorySearchText = new List(); const int MaxHistoryCount = 10; private void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { TextBox text = e.Source as TextBox; if (text == null) { return; } if (string.IsNullOrEmpty(text.Text)) { return; } (DataContext as SearchContentViewModel).SearchText(text.Text); HistorySearchText.Remove(text.Text); if (HistorySearchText.Count >= MaxHistoryCount) { HistorySearchText.RemoveAt(HistorySearchText.Count - 1); } HistorySearchText.Insert(0, text.Text); } } /// /// 控制收起与展开 /// private void BtnExptend_Click(object sender, RoutedEventArgs e) { var btn = sender as PathButton; if (btn == null) return; btn.IsChecked = !btn.IsChecked; var item = (sender as FrameworkElement).DataContext as CollectionViewGroup; if (item != null) { foreach (object item2 in item.Items) { SearchItem searchItem = item2 as SearchItem; if (searchItem.TextProperty.ItemVisibility == Visibility.Visible) { searchItem.TextProperty.ItemVisibility = Visibility.Collapsed; } else { searchItem.TextProperty.ItemVisibility = Visibility.Visible; } } } } private void Create_Click(object sender, RoutedEventArgs e) { MenuItem menuItem = sender as MenuItem; if (menuItem == null) { return; } foreach (object item in SearchResultList.SelectedItems) { (DataContext as SearchContentViewModel).CreateAnnotate(item, menuItem.Tag.ToString()); } } private void TextBoxEx_Initialized(object sender, EventArgs e) { var btn = sender as TextBoxEx; if (btn != null) { btn.ContextMenu = null; } } private void PathButton_Click(object sender, RoutedEventArgs e) { ContextSearchText.PlacementTarget = SearchText; ContextSearchText.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom; ContextSearchText.IsOpen = true; UpDataItem(); } private void UpDataItem() { foreach (var item in ContextSearchText.Items) { if (item is MenuItem) { (item as MenuItem).Click -= SearchContent_Click; } } ContextSearchText.Items.Clear(); ContextSearchText.Items.Add(IgnoreItem); if (HistorySearchText.Count > 0) { ContextSearchText.Items.Add(IgnoreSeparator); } foreach (var item in HistorySearchText) { MenuItem Searchitem = new MenuItem(); Searchitem.Height = 32; Searchitem.Header = item; Searchitem.Padding = new Thickness(0, 7, 0, 7); Searchitem.Click += SearchContent_Click; ContextSearchText.Items.Add(Searchitem); } if (HistorySearchText.Count > 0) { ContextSearchText.Items.Add(ClearSeparator); ContextSearchText.Items.Add(ClearItem); } } private void SearchContent_Click(object sender, RoutedEventArgs e) { MenuItem item = sender as MenuItem; if (item != null) { SearchText.Text = item.Header.ToString(); (DataContext as SearchContentViewModel).SearchText(SearchText.Text); } } private void ClearItem_Click(object sender, RoutedEventArgs e) { HistorySearchText.Clear(); UpDataItem(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { SearchText.Focus(); } private void UserControl_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { SearchResultList.SelectedIndex = -1; e.Handled = true; } } } }