WPF从动态集ListViewItem获取鼠标悬停事件



如何从动态集ListViewItems获取MouseOver事件?

XAML:

<ListView Name="ListView" x:FieldModifier="public" ItemsSource="{Binding FileDataList}" Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="8" Grid.RowSpan="12">       
<ListView.View>
<GridView>
<GridViewColumn Header="Filename" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
<GridViewColumn Header="Change date" DisplayMemberBinding="{Binding Date}" Width="Auto"/>
<GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" Width="Auto"/>
<GridViewColumn Header="Size" DisplayMemberBinding="{Binding Size}" Width="Auto"/>
</GridView>
</ListView.View>            
</ListView>

代码背后:

fileDataList.Add(new FileData() { Name = System.IO.Path.GetFileNameWithoutExtension(filename), Date = File.GetLastWriteTime(filename), Type = System.IO.Path.GetExtension(filename), Size = GetFileSize(filename) });

以下是列表:

public List<string> files = new List<string>();
private ObservableCollection<FileData> fileDataList = new ObservableCollection<FileData>();

ListView的资源中设置以下样式:

<ListView.Resources>
<Style TargetType="ListViewItem">
<EventSetter Event="MouseEnter" Handler="ListViewItem_MouseEnter" />
<EventSetter Event="MouseLeave" Handler="ListViewItem_MouseLeave" />
</Style>
</ListView.Resources>

在代码后面使用以下代码来确定当前悬停的项目。

private ListViewItem _currentItem = null;
private void ListViewItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
var item = sender as ListViewItem;
if (!Equals(_currentItem, item))
{
_currentItem = item;
// code to update window 
}
}
private void ListViewItem_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
_currentItem = null;
}

相关内容

  • 没有找到相关文章

最新更新