Wpf Highlight ListViewItem On Drag Drop

In my WPF application, I need to highlight the ListViewItem whenever something is about to fall on it. I override OnDragEnter, OnDragOver, OnDragLeave, etc. ListViewItem to apply my styles (say background changes). It is working fine. but after removing somthing in the listview element, when I click the list elements, the selection and mouse effects are not working properly. How can i solve this?

public class CustomListViewItem : ListViewItem
{
    protected override void OnDragOver(System.Windows.DragEventArgs e)
    {
        this.Background = Brushes.Green;
        base.OnDragOver(e);
    }

    protected override void OnDragEnter(System.Windows.DragEventArgs e)
    {
        this.Background = Brushes.Green;
        base.OnDragEnter(e);
    }

    protected override void OnDragLeave(System.Windows.DragEventArgs e)
    {
        if (!this.IsSelected)
        {
            this.Background = Brushes.Transparent;
            this.BorderBrush = Brushes.Transparent;
        }
        base.OnDragLeave(e);
    }
}
+3
source share
1 answer

After running DragDrop, your local value takes precedence over the selection and mouseover effects in style (see the list of dependency property preferences ).

Try the DependencyObject.ClearValue Method :

protected override void OnDragLeave(System.Windows.DragEventArgs e)
{
    if (!this.IsSelected)
    {
        this.ClearValue(BackgroundProperty);
        this.ClearValue(BorderBrushProperty);
    }
    base.OnDragLeave(e);
}
+3
source

All Articles