BookmarkContent.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using PDF_Office.Helper;
  2. using PDF_Office.ViewModels.BOTA;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace PDF_Office.Views.BOTA
  18. {
  19. /// <summary>
  20. /// BookmarkContent.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class BookmarkContent : UserControl
  23. {
  24. private BookmarkContentViewModel viewModel = null;
  25. private ListBoxItem histotyListBoxItem = null;
  26. public BookmarkContent()
  27. {
  28. InitializeComponent();
  29. viewModel = this.DataContext as BookmarkContentViewModel;
  30. }
  31. private void TxtTitleInput_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  32. {
  33. if (sender is TextBox && IsLoaded)
  34. {
  35. TextBox textBox = sender as TextBox;
  36. if (textBox.Visibility == Visibility.Visible)
  37. {
  38. textBox.Focus();
  39. }
  40. }
  41. }
  42. private void ListViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  43. {
  44. object[] objects = new object[] { sender, e };
  45. TextBlock textBlock = null;
  46. ListBoxItem listBoxItem = (sender as ListBoxItem);
  47. if (e.LeftButton == MouseButtonState.Pressed)
  48. {
  49. if (e.ClickCount >= 2)
  50. {
  51. histotyListBoxItem = listBoxItem;
  52. if (e.OriginalSource is TextBlock)
  53. {
  54. textBlock = (TextBlock)e.OriginalSource;
  55. if (textBlock != null)
  56. {
  57. listBoxItem.IsSelected = true;
  58. listBoxItem.IsSelected = true;
  59. listBoxItem.Focus();
  60. textBlock.Visibility = Visibility.Collapsed;
  61. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  62. textBox.Dispatcher.BeginInvoke(new Action(() =>
  63. {
  64. textBox.Focus();
  65. textBox.SelectAll();
  66. }));
  67. //textBox.PreviewMouseDown += new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
  68. //TxtTitleInput_PreviewMouseLeftButtonDown(textBox, mouse);
  69. }
  70. }
  71. }
  72. else if (e.ClickCount == 1)
  73. {
  74. //var pos = e.GetPosition(histotyListBoxItem);
  75. //var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  76. //if (result == null)
  77. //{
  78. // histotyListBoxItem.IsSelected = false;
  79. // textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  80. // textBlock.Visibility = Visibility.Visible;
  81. // textBlock.Focusable = true;
  82. //}
  83. if (histotyListBoxItem != listBoxItem)
  84. {
  85. if (histotyListBoxItem != null)
  86. {
  87. histotyListBoxItem.IsSelected = false;
  88. textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  89. textBlock.Visibility = Visibility.Visible;
  90. textBlock.Focusable = true;
  91. }
  92. }
  93. viewModel.ListViewItemMouseDownCommand.Execute(sender);
  94. }
  95. }
  96. else if (e.RightButton == MouseButtonState.Pressed)
  97. {
  98. listBoxItem.IsSelected = true;
  99. listBoxItem.Focus();
  100. }
  101. }
  102. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  103. {
  104. ListBoxItem listItem = sender as ListBoxItem;
  105. if (listItem != null)
  106. {
  107. viewModel.LostFocusCommand.Execute(listItem);
  108. TextBlock textBox = CommonHelper.FindVisualChild<TextBlock>(listItem);
  109. if (textBox.Visibility == Visibility.Collapsed)
  110. {
  111. listItem.IsSelected = true;
  112. }
  113. else
  114. {
  115. listItem.IsSelected = false;
  116. }
  117. }
  118. }
  119. private void BookMarkListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  120. {
  121. var pos = e.GetPosition(BookMarkListView);
  122. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  123. if (result != null)
  124. {
  125. //获取当前鼠标指针下的容器
  126. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  127. if (listBoxItem == null)
  128. {
  129. if (BookMarkListView.SelectedItem != null)
  130. {
  131. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  132. item.IsSelected = false;
  133. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  134. box.Visibility = Visibility.Visible;
  135. BookMarkListView.SelectedItems.Clear();
  136. }
  137. if (histotyListBoxItem != null)
  138. {
  139. var pos1 = e.GetPosition(histotyListBoxItem);
  140. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  141. if (result1 == null)
  142. {
  143. histotyListBoxItem.IsSelected = false;
  144. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  145. textBlock.Visibility = Visibility.Visible;
  146. textBlock.Focusable = true;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. listBoxItem.Focus();
  153. }
  154. }
  155. BookMarkListView.Focus();
  156. }
  157. }
  158. }