AnnotationReplyListControl.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. namespace ComPDFKit.Controls.PDFControlUI
  7. {
  8. /// <summary>
  9. /// Interaction logic for AnnotationReplyListControl.xaml
  10. /// </summary>
  11. public partial class AnnotationReplyListControl : UserControl
  12. {
  13. public ObservableCollection<CPDFAnnotationListUI.ReplyData> ReplyListSource
  14. {
  15. get => (ObservableCollection<CPDFAnnotationListUI.ReplyData>)GetValue(ReplyListProperty);
  16. set => SetValue(ReplyListProperty, value);
  17. }
  18. /// <summary>
  19. /// The source of the reply list.
  20. /// </summary>
  21. public static readonly DependencyProperty ReplyListProperty = DependencyProperty.Register(
  22. nameof(ReplyListSource),
  23. typeof(ObservableCollection<CPDFAnnotationListUI.ReplyData>),
  24. typeof(AnnotationReplyListControl),
  25. new PropertyMetadata(null, OnReplyListSourceChanged));
  26. private static void OnReplyListSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  27. {
  28. var newReplyListSource = e.NewValue as ObservableCollection<CPDFAnnotationListUI.ReplyData>;
  29. if (d is AnnotationReplyListControl control)
  30. {
  31. control.ReplyList.ItemsSource = newReplyListSource;
  32. }
  33. }
  34. public bool IsShowInput
  35. {
  36. get => (bool)GetValue(IsShowInputProperty);
  37. set => SetValue(IsShowInputProperty, value);
  38. }
  39. /// <summary>
  40. /// The visibility of the reply input.
  41. /// </summary>
  42. public static readonly DependencyProperty IsShowInputProperty = DependencyProperty.Register(
  43. nameof(IsShowInput),
  44. typeof(bool),
  45. typeof(AnnotationReplyListControl),
  46. new PropertyMetadata(false, OnIsShowInputChanged));
  47. public static event EventHandler ReplyListChanged;
  48. public static event EventHandler<CPDFAnnotationListUI.ReplyData> ReplyDeleted;
  49. private static void OnIsShowInputChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  50. {
  51. if (d is AnnotationReplyListControl control)
  52. {
  53. control.SetReplyInputVisibility((bool)e.NewValue);
  54. }
  55. }
  56. public bool IsShowList
  57. {
  58. get => (bool)GetValue(IsShowListProperty);
  59. set => SetValue(IsShowListProperty, value);
  60. }
  61. /// <summary>
  62. /// The visibility of the reply list.
  63. /// </summary>
  64. public static readonly DependencyProperty IsShowListProperty = DependencyProperty.Register(
  65. nameof(IsShowList),
  66. typeof(bool),
  67. typeof(AnnotationReplyListControl),
  68. new PropertyMetadata(false, OnIsShowListChanged));
  69. public void SetReplyInputVisibility(bool isVisible)
  70. {
  71. if (isVisible)
  72. {
  73. ReplyGrid.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Auto);
  74. ReplyGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
  75. }
  76. else
  77. {
  78. ReplyGrid.RowDefinitions[0].Height = new GridLength(0);
  79. ReplyGrid.RowDefinitions[1].Height = new GridLength(0);
  80. }
  81. }
  82. private static void OnIsShowListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  83. {
  84. if (d is AnnotationReplyListControl control)
  85. {
  86. control.SetReplyListVisibility((bool)e.NewValue);
  87. }
  88. }
  89. public void SetReplyListVisibility(bool isVisible)
  90. {
  91. ReplyGrid.RowDefinitions[2].Height = isVisible ? new GridLength(1, GridUnitType.Auto) : new GridLength(0);
  92. }
  93. public static void InvokeReplyListChanged()
  94. {
  95. ReplyListChanged?.Invoke(null, EventArgs.Empty);
  96. }
  97. public static void InvokeReplyDeleted(CPDFAnnotationListUI.ReplyData replyData)
  98. {
  99. ReplyDeleted?.Invoke(null, replyData);
  100. }
  101. public AnnotationReplyListControl()
  102. {
  103. InitializeComponent();
  104. }
  105. private void ReplyButton_Click(object sender, RoutedEventArgs e)
  106. {
  107. if (sender is Button button)
  108. {
  109. if (button.DataContext is AnnotationBindData annotBindData)
  110. {
  111. if (!string.IsNullOrEmpty(InputTxb.Text))
  112. {
  113. var replyAnnot = annotBindData.BindProperty.Annotation.CreateReplyAnnotation();
  114. replyAnnot.SetContent(InputTxb.Text);
  115. replyAnnot.SetAuthor(Data.CPDFAnnotationData.Author);
  116. annotBindData.BindProperty.ReplyList.Add(new CPDFAnnotationListUI.ReplyData
  117. {
  118. ReplyAnnotation = replyAnnot
  119. });
  120. InputTxb.Text = string.Empty;
  121. InvokeReplyListChanged();
  122. }
  123. }
  124. }
  125. }
  126. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  127. {
  128. IsShowInput = false;
  129. }
  130. private void ContentBox_LostFocus(object sender, RoutedEventArgs e)
  131. {
  132. if (sender is TextBox textBox)
  133. {
  134. textBox.Visibility = Visibility.Collapsed;
  135. if (textBox.DataContext is CPDFAnnotationListUI.ReplyData replyData)
  136. {
  137. replyData.ReplyAnnotation.SetContent(textBox.Text);
  138. InvokeReplyListChanged();
  139. }
  140. }
  141. }
  142. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  143. {
  144. ReplyDeleted -= AnnotationReplyListControl_ReplyDeleted;
  145. ReplyDeleted += AnnotationReplyListControl_ReplyDeleted;
  146. }
  147. private void AnnotationReplyListControl_ReplyDeleted(object sender, CPDFAnnotationListUI.ReplyData e)
  148. {
  149. ReplyListSource.Remove(e);
  150. }
  151. }
  152. public class ShowContentBoxCommand : ICommand
  153. {
  154. public event EventHandler CanExecuteChanged;
  155. public bool CanExecute(object parameter)
  156. {
  157. return true;
  158. }
  159. public void Execute(object parameter)
  160. {
  161. if (parameter is TextBox textBox)
  162. {
  163. textBox.Visibility = Visibility.Visible;
  164. textBox.Focus();
  165. textBox.SelectAll();
  166. }
  167. }
  168. }
  169. public class DeleteReplyCommand : ICommand
  170. {
  171. public event EventHandler CanExecuteChanged;
  172. public bool CanExecute(object parameter)
  173. {
  174. return true;
  175. }
  176. public void Execute(object parameter)
  177. {
  178. if (parameter is CPDFAnnotationListUI.ReplyData replyData)
  179. {
  180. replyData.ReplyAnnotation.RemoveAnnot();
  181. AnnotationReplyListControl.InvokeReplyDeleted(replyData);
  182. AnnotationReplyListControl.InvokeReplyListChanged();
  183. }
  184. }
  185. }
  186. }