using System.ComponentModel; using System.Drawing; using System.Windows; using System.Windows.Forms; using System.Windows.Media; namespace CustomControls { [DesignTimeVisible(false)] public class OwnerDrawItem : System.Windows.Controls.Control { public bool IsSelectedChanged { get { return false; } set { SetValue(IsSelectedChangedProperty, value); } } public readonly static DependencyProperty IsSelectedChangedProperty = DependencyProperty.Register( "IsSelectedChanged", typeof(bool), typeof(OwnerDrawItem), new UIPropertyMetadata(false, (o, e) => (o as OwnerDrawItem).InvalidateVisual())); public new bool IsEnabledChanged { get { return false; } set { SetValue(IsEnabledChangedProperty, value); } } public readonly static DependencyProperty IsEnabledChangedProperty = DependencyProperty.Register( "IsEnabledChanged", typeof(bool), typeof(OwnerDrawItem), new UIPropertyMetadata(false, (o, e) => (o as OwnerDrawItem).InvalidateVisual())); public bool IsMouseOverChanged { get { return false; } set { SetValue(IsMouseOverChangedProperty, value); } } public readonly static DependencyProperty IsMouseOverChangedProperty = DependencyProperty.Register( "IsMouseOverChanged", typeof(bool), typeof(OwnerDrawItem), new UIPropertyMetadata(false, (o, e) => (o as OwnerDrawItem).InvalidateVisual())); // this is used to invalidate item when its value changes (case: combobox drawing its value in the cbx part) public object Value { get { return GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public readonly static DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(object), typeof(OwnerDrawItem), new UIPropertyMetadata(null, (o, e) => (o as OwnerDrawItem).InvalidateVisual())); protected void OnDrawItem(DrawingContext drawingContext, int index, bool isSelected, bool isEnabled, bool isMouseOver, bool comboboxEdit, DrawItemEventHandler drawItem) { if (ActualWidth == 0 || ActualHeight == 0) return; var state = DrawItemState.Default; if (isSelected) state |= DrawItemState.Selected; if (!isEnabled) state |= DrawItemState.Disabled; if (isMouseOver) state |= DrawItemState.HotLight; if (comboboxEdit) state |= DrawItemState.ComboBoxEdit; var scale = Helper.GetDpiScale(this); using (var bmp = new Bitmap((int)(ActualWidth * scale), (int)(ActualHeight * scale))) { using (var g = Graphics.FromImage(bmp)) using (var font = Helper.GetFont(this)) { var args = new DrawItemEventArgs(g, font, new Rectangle(0, 0, bmp.Width, bmp.Height), index, state); drawItem.Invoke(this, args); } var imageSource = Helper.GetImage(bmp); if (FlowDirection == FlowDirection.RightToLeft) drawingContext.PushTransform(new MatrixTransform(-1, 0, 0, 1, ActualWidth, 0)); drawingContext.DrawImage(imageSource, new Rect(0, 0, ActualWidth, ActualHeight)); } } } }