在我的WPF应用程序中,每当有什么东西要放在它上面时我都需要突出显示ListViewItem。我重写ListViewItem的OnDragEnter,OnDragOver,OnDragLeave等来应用我的样式(比如更改背景)。它工作正常。但是在列表视图项目上删除某些内容后,当我单击listview项目时,选择和鼠标悬停效果无法正常工作。我该如何解决这个问题?
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);
}
}
答案 0 :(得分:4)
执行DragDrop后,您的本地值优先于样式的选择和鼠标悬停效果(请参阅Dependency Property Setting Precedence List )。
尝试DependencyObject.ClearValue Method:
protected override void OnDragLeave(System.Windows.DragEventArgs e)
{
if (!this.IsSelected)
{
this.ClearValue(BackgroundProperty);
this.ClearValue(BorderBrushProperty);
}
base.OnDragLeave(e);
}