WritableComboBox.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using ImTools;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace PDF_Office.CustomControl
  19. {
  20. /// <summary>
  21. /// WritableComboBox.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class WritableComboBox : UserControl
  24. {
  25. public bool IsCurrentPage
  26. {
  27. get { return (bool)GetValue(IsCurrentPageProperty); }
  28. set { SetValue(IsCurrentPageProperty, value); }
  29. }
  30. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  31. public static readonly DependencyProperty IsCurrentPageProperty =
  32. DependencyProperty.Register("IsCurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  33. public Visibility IsAllPageVisible
  34. {
  35. get { return (Visibility)GetValue(IsAllPageVisibleProperty); }
  36. set
  37. {
  38. SetValue(IsAllPageVisibleProperty, value);
  39. }
  40. }
  41. // Using a DependencyProperty as the backing store for IsAllPageVisible. This enables animation, styling, binding, etc...
  42. public static readonly DependencyProperty IsAllPageVisibleProperty =
  43. DependencyProperty.Register("IsAllPageVisible", typeof(Visibility), typeof(WritableComboBox), new PropertyMetadata(Visibility.Visible, (d, e) =>
  44. {
  45. if ((Visibility)e.NewValue != Visibility.Visible)
  46. {
  47. (d as WritableComboBox).SetIndexByVisiblity((Visibility)e.NewValue);
  48. }
  49. }));
  50. private void SetIndexByVisiblity(Visibility visible)
  51. {
  52. writableComboBox.SelectedIndex = 1;
  53. }
  54. public bool CurrentPage
  55. {
  56. get { return (bool)GetValue(CurrentPageProperty); }
  57. set { SetValue(CurrentPageProperty, value); }
  58. }
  59. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  60. public static readonly DependencyProperty CurrentPageProperty =
  61. DependencyProperty.Register("CurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  62. public bool EvenPageIsEnabled
  63. {
  64. get { return (bool)GetValue(EvenPageIsEnabledProperty); }
  65. set { SetValue(EvenPageIsEnabledProperty, value); }
  66. }
  67. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  68. public static readonly DependencyProperty EvenPageIsEnabledProperty =
  69. DependencyProperty.Register("EvenPageIsEnabled", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(true));
  70. public string SelectedIndex
  71. {
  72. get { return (string)GetValue(SelectedIndexProperty); }
  73. set { SetValue(SelectedIndexProperty, value); }
  74. }
  75. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  76. public static readonly DependencyProperty SelectedIndexProperty =
  77. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  78. public string Text
  79. {
  80. get { return (string)GetValue(TextProperty); }
  81. set { SetValue(TextProperty, value); }
  82. }
  83. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  84. public static readonly DependencyProperty TextProperty =
  85. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  86. public ItemCollection Items
  87. {
  88. get { return (ItemCollection)GetValue(ItemsProperty); }
  89. set { SetValue(ItemsProperty, value); }
  90. }
  91. public static readonly DependencyProperty ItemsProperty =
  92. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  93. /// <summary>
  94. /// 把子控件的事件传递出去,方便绑定
  95. /// </summary>
  96. public event RoutedEventHandler SelectionChanged;
  97. /// <summary>
  98. /// 把子控件的事件传递出去,方便绑定
  99. /// </summary>
  100. public event RoutedEventHandler TextChanged;
  101. public WritableComboBox()
  102. {
  103. InitializeComponent();
  104. this.Items = this.writableComboBox.Items;
  105. }
  106. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  107. {
  108. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  109. {
  110. if (this.writableComboBox.ActualWidth == 0) { this.writableTextBox.Width = 210; this.writableTextBox.Visibility = Visibility.Visible; return; }
  111. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 18;
  112. Trace.WriteLine(this.writableComboBox.ActualWidth);
  113. this.writableTextBox.Visibility = Visibility.Visible;
  114. }
  115. else
  116. {
  117. if (this.writableTextBox != null)
  118. {
  119. this.writableTextBox.Visibility = Visibility.Hidden;
  120. }
  121. }
  122. if (this.writableComboBox.Items.Count == 5)
  123. {
  124. if (this.writableComboBox.SelectedIndex == 1)
  125. { IsCurrentPage = true; }
  126. else
  127. {
  128. IsCurrentPage = false;
  129. }
  130. }
  131. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  132. SelectionChanged?.Invoke(sender, e);
  133. }
  134. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  135. {
  136. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  137. {
  138. Text = this.writableTextBox.Text;
  139. }
  140. else { Text = ""; }
  141. TextChanged?.Invoke(sender, e);
  142. }
  143. }
  144. }