WritableComboBox.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 string SelectedIndex
  63. {
  64. get { return (string)GetValue(SelectedIndexProperty); }
  65. set { SetValue(SelectedIndexProperty, value); }
  66. }
  67. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  68. public static readonly DependencyProperty SelectedIndexProperty =
  69. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  70. public string Text
  71. {
  72. get { return (string)GetValue(TextProperty); }
  73. set { SetValue(TextProperty, value); }
  74. }
  75. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  76. public static readonly DependencyProperty TextProperty =
  77. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  78. public ItemCollection Items
  79. {
  80. get { return (ItemCollection)GetValue(ItemsProperty); }
  81. set { SetValue(ItemsProperty, value); }
  82. }
  83. public static readonly DependencyProperty ItemsProperty =
  84. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  85. /// <summary>
  86. /// 把子控件的事件传递出去,方便绑定
  87. /// </summary>
  88. public event RoutedEventHandler SelectionChanged;
  89. /// <summary>
  90. /// 把子控件的事件传递出去,方便绑定
  91. /// </summary>
  92. public event RoutedEventHandler TextChanged;
  93. public WritableComboBox()
  94. {
  95. InitializeComponent();
  96. this.Items = this.writableComboBox.Items;
  97. }
  98. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  99. {
  100. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  101. {
  102. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 18;
  103. Trace.WriteLine(this.writableComboBox.ActualWidth);
  104. this.writableTextBox.Visibility = Visibility.Visible;
  105. }
  106. else
  107. {
  108. if (this.writableTextBox != null)
  109. {
  110. this.writableTextBox.Visibility = Visibility.Hidden;
  111. }
  112. }
  113. if (this.writableComboBox.Items.Count == 5)
  114. {
  115. if (this.writableComboBox.SelectedIndex == 1)
  116. { IsCurrentPage = true; }
  117. else
  118. {
  119. IsCurrentPage = false;
  120. }
  121. }
  122. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  123. SelectionChanged?.Invoke(sender, e);
  124. }
  125. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  126. {
  127. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  128. {
  129. Text = this.writableTextBox.Text;
  130. }
  131. else { Text = ""; }
  132. TextChanged?.Invoke(sender, e);
  133. }
  134. }
  135. }