OwnerDrawItem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. using System.Windows;
  4. using System.Windows.Forms;
  5. using System.Windows.Media;
  6. namespace CustomControls
  7. {
  8. [DesignTimeVisible(false)]
  9. public class OwnerDrawItem : System.Windows.Controls.Control
  10. {
  11. public bool IsSelectedChanged
  12. {
  13. get { return false; }
  14. set { SetValue(IsSelectedChangedProperty, value); }
  15. }
  16. public readonly static DependencyProperty IsSelectedChangedProperty = DependencyProperty.Register(
  17. "IsSelectedChanged", typeof(bool), typeof(OwnerDrawItem), new UIPropertyMetadata(false, (o, e) => (o as OwnerDrawItem).InvalidateVisual()));
  18. public new bool IsEnabledChanged
  19. {
  20. get { return false; }
  21. set { SetValue(IsEnabledChangedProperty, value); }
  22. }
  23. public readonly static DependencyProperty IsEnabledChangedProperty = DependencyProperty.Register(
  24. "IsEnabledChanged", typeof(bool), typeof(OwnerDrawItem), new UIPropertyMetadata(false, (o, e) => (o as OwnerDrawItem).InvalidateVisual()));
  25. public bool IsMouseOverChanged
  26. {
  27. get { return false; }
  28. set { SetValue(IsMouseOverChangedProperty, value); }
  29. }
  30. public readonly static DependencyProperty IsMouseOverChangedProperty = DependencyProperty.Register(
  31. "IsMouseOverChanged", typeof(bool), typeof(OwnerDrawItem), new UIPropertyMetadata(false, (o, e) => (o as OwnerDrawItem).InvalidateVisual()));
  32. // this is used to invalidate item when its value changes (case: combobox drawing its value in the cbx part)
  33. public object Value
  34. {
  35. get { return GetValue(ValueProperty); }
  36. set { SetValue(ValueProperty, value); }
  37. }
  38. public readonly static DependencyProperty ValueProperty = DependencyProperty.Register(
  39. "Value", typeof(object), typeof(OwnerDrawItem), new UIPropertyMetadata(null, (o, e) => (o as OwnerDrawItem).InvalidateVisual()));
  40. protected void OnDrawItem(DrawingContext drawingContext, int index, bool isSelected, bool isEnabled, bool isMouseOver, bool comboboxEdit, DrawItemEventHandler drawItem)
  41. {
  42. if (ActualWidth == 0 || ActualHeight == 0)
  43. return;
  44. var state = DrawItemState.Default;
  45. if (isSelected)
  46. state |= DrawItemState.Selected;
  47. if (!isEnabled)
  48. state |= DrawItemState.Disabled;
  49. if (isMouseOver)
  50. state |= DrawItemState.HotLight;
  51. if (comboboxEdit)
  52. state |= DrawItemState.ComboBoxEdit;
  53. var scale = Helper.GetDpiScale(this);
  54. using (var bmp = new Bitmap((int)(ActualWidth * scale), (int)(ActualHeight * scale)))
  55. {
  56. using (var g = Graphics.FromImage(bmp))
  57. using (var font = Helper.GetFont(this))
  58. {
  59. var args = new DrawItemEventArgs(g, font, new Rectangle(0, 0, bmp.Width, bmp.Height), index, state);
  60. drawItem.Invoke(this, args);
  61. }
  62. var imageSource = Helper.GetImage(bmp);
  63. if (FlowDirection == FlowDirection.RightToLeft)
  64. drawingContext.PushTransform(new MatrixTransform(-1, 0, 0, 1, ActualWidth, 0));
  65. drawingContext.DrawImage(imageSource, new Rect(0, 0, ActualWidth, ActualHeight));
  66. }
  67. }
  68. }
  69. }