WritableComboBox.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 { SetValue(IsAllPageVisibleProperty, value);
  37. }
  38. }
  39. // Using a DependencyProperty as the backing store for IsAllPageVisible. This enables animation, styling, binding, etc...
  40. public static readonly DependencyProperty IsAllPageVisibleProperty =
  41. DependencyProperty.Register("IsAllPageVisible", typeof(Visibility), typeof(WritableComboBox), new PropertyMetadata(Visibility.Visible,(d,e)=> {
  42. if((Visibility)e.NewValue!=Visibility.Visible)
  43. {
  44. (d as WritableComboBox).SetIndexByVisiblity((Visibility)e.NewValue);
  45. }
  46. }));
  47. private void SetIndexByVisiblity(Visibility visible)
  48. {
  49. writableComboBox.SelectedIndex = 1;
  50. }
  51. public bool CurrentPage
  52. {
  53. get { return (bool)GetValue(CurrentPageProperty); }
  54. set { SetValue(CurrentPageProperty, value); }
  55. }
  56. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  57. public static readonly DependencyProperty CurrentPageProperty =
  58. DependencyProperty.Register("CurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  59. public string SelectedIndex
  60. {
  61. get { return (string)GetValue(SelectedIndexProperty); }
  62. set { SetValue(SelectedIndexProperty, value); }
  63. }
  64. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  65. public static readonly DependencyProperty SelectedIndexProperty =
  66. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  67. public string Text
  68. {
  69. get { return (string)GetValue(TextProperty); }
  70. set { SetValue(TextProperty, value); }
  71. }
  72. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  73. public static readonly DependencyProperty TextProperty =
  74. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  75. /// <summary>
  76. /// 把子控件的事件传递出去,方便绑定
  77. /// </summary>
  78. public event RoutedEventHandler SelectionChanged;
  79. /// <summary>
  80. /// 把子控件的事件传递出去,方便绑定
  81. /// </summary>
  82. public event RoutedEventHandler TextChanged;
  83. public WritableComboBox()
  84. {
  85. InitializeComponent();
  86. }
  87. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  88. {
  89. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  90. {
  91. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 18;
  92. Trace.WriteLine(this.writableComboBox.ActualWidth);
  93. this.writableTextBox.Visibility = Visibility.Visible;
  94. }
  95. else
  96. {
  97. if (this.writableTextBox != null)
  98. {
  99. this.writableTextBox.Visibility = Visibility.Hidden;
  100. }
  101. }
  102. if (this.writableComboBox.Items.Count == 5)
  103. {
  104. if (this.writableComboBox.SelectedIndex == 1)
  105. { IsCurrentPage = true; }
  106. else
  107. {
  108. IsCurrentPage = false;
  109. }
  110. }
  111. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  112. SelectionChanged?.Invoke(sender,e);
  113. }
  114. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  115. {
  116. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  117. {
  118. Text = this.writableTextBox.Text;
  119. }
  120. else { Text = ""; }
  121. TextChanged?.Invoke(sender,e);
  122. }
  123. }
  124. }