OwnerDrawListBox.xaml.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Forms;
  6. using System.Windows.Media;
  7. namespace CustomControls
  8. {
  9. [DesignTimeVisible(false)]
  10. public partial class OwnerDrawListBox : System.Windows.Controls.ListBox
  11. {
  12. private System.Drawing.Graphics measureGraphics;
  13. private System.Drawing.Graphics MeasureGraphics => measureGraphics ??= System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
  14. public bool OwnerDraw
  15. {
  16. get { return (bool)GetValue(OwnerDrawProperty); }
  17. set { SetValue(OwnerDrawProperty, value); }
  18. }
  19. public readonly static DependencyProperty OwnerDrawProperty = DependencyProperty.Register(
  20. "OwnerDraw", typeof(bool), typeof(OwnerDrawListBox), new UIPropertyMetadata(false, (o, e) =>
  21. {
  22. if ((bool)e.NewValue == true)
  23. {
  24. var listBox = (OwnerDrawListBox)o;
  25. listBox.ItemContainerStyle = (Style)listBox.Resources["ItemContainerStyle1"];
  26. listBox.ItemHeight = 15;
  27. };
  28. }));
  29. public int ItemHeight
  30. {
  31. get { return (int)GetValue(ItemHeightProperty); }
  32. set { SetValue(ItemHeightProperty, value); }
  33. }
  34. public readonly static DependencyProperty ItemHeightProperty = DependencyProperty.Register(
  35. "ItemHeight", typeof(int), typeof(OwnerDrawListBox), new UIPropertyMetadata(19));
  36. public event DrawItemEventHandler DrawItem;
  37. public event MeasureItemEventHandler MeasureItem;
  38. internal void OnDrawItem(object sender, DrawItemEventArgs args) => DrawItem?.Invoke(this, args);
  39. internal Size OnMeasureItem(int index, Size size)
  40. {
  41. var args = new MeasureItemEventArgs(MeasureGraphics, index, ItemHeight);
  42. MeasureItem?.Invoke(this, args);
  43. size.Height = args.ItemHeight;
  44. return size;
  45. }
  46. public OwnerDrawListBox()
  47. {
  48. InitializeComponent();
  49. }
  50. }
  51. [DesignTimeVisible(false)]
  52. public class OwnerDrawListBoxItem : OwnerDrawItem
  53. {
  54. protected override void OnRender(DrawingContext drawingContext)
  55. {
  56. var listItem = WpfHelper.FindAncestor<System.Windows.Controls.ListBoxItem>(this);
  57. var listBox = ItemsControl.ItemsControlFromItemContainer(listItem) as OwnerDrawListBox;
  58. int index = listBox.ItemContainerGenerator.IndexFromContainer(listItem);
  59. OnDrawItem(drawingContext, index, listItem.IsSelected, listItem.IsEnabled, listItem.IsMouseOver, false, listBox.OnDrawItem);
  60. }
  61. protected override Size MeasureOverride(Size constraint)
  62. {
  63. var listItem = WpfHelper.FindAncestor<System.Windows.Controls.ListBoxItem>(this);
  64. var listBox = ItemsControl.ItemsControlFromItemContainer(listItem) as OwnerDrawListBox;
  65. int index = listBox.ItemContainerGenerator.IndexFromContainer(listItem);
  66. return listBox.OnMeasureItem(index, base.MeasureOverride(constraint));
  67. }
  68. }
  69. }