FeaturesListControl.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  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 Compdfkit_Tools.PDFControl
  19. {
  20. /// <summary>
  21. /// Interaction logic for FeaturesListControl.xaml
  22. /// </summary>
  23. public partial class FeaturesListControl : UserControl
  24. {
  25. public event EventHandler<CustomItem> SelectionChanged;
  26. public static readonly DependencyProperty ItemsProperty =
  27. DependencyProperty.Register("Items", typeof(ObservableCollection<CustomItem>), typeof(FeaturesListControl), new PropertyMetadata(null));
  28. public ObservableCollection<CustomItem> Items
  29. {
  30. get { return (ObservableCollection<CustomItem>)GetValue(ItemsProperty); }
  31. set { SetValue(ItemsProperty, value); }
  32. }
  33. public FeaturesListControl()
  34. {
  35. this.DataContext = this;
  36. InitializeComponent();
  37. Items = new ObservableCollection<CustomItem>();
  38. }
  39. private void FeaturesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  40. {
  41. var listBox = sender as ListBox;
  42. if (listBox != null && listBox.SelectedIndex != -1)
  43. {
  44. SelectionChanged?.Invoke(sender, Items[listBox.SelectedIndex]);
  45. listBox.SelectedIndex = -1;
  46. }
  47. }
  48. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  49. {
  50. FeaturesListBox.SelectionChanged += FeaturesListBox_SelectionChanged;
  51. }
  52. }
  53. public class CustomItem : Control, INotifyPropertyChanged
  54. {
  55. private string _titleText;
  56. public string TitleText
  57. {
  58. get => _titleText;
  59. set
  60. {
  61. UpdateProper(ref _titleText, value);
  62. }
  63. }
  64. private string _descriptionText;
  65. public string DescriptionText
  66. {
  67. get => _descriptionText;
  68. set
  69. {
  70. UpdateProper(ref _descriptionText, value);
  71. }
  72. }
  73. public event PropertyChangedEventHandler PropertyChanged;
  74. protected void UpdateProper<T>(ref T properValue,
  75. T newValue,
  76. [CallerMemberName] string properName = "")
  77. {
  78. if (object.Equals(properValue, newValue))
  79. return;
  80. properValue = newValue;
  81. OnPropertyChanged(properName);
  82. }
  83. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  84. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  85. }
  86. }