猿问

WPF 从动态集 ListViewItem 中获取 MouseHover 事件

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


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>();


收到一只叮咚
浏览 357回答 1
1回答

明月笑刀无情

在ListView的资源中设置以下样式:&nbsp;<ListView.Resources>&nbsp; &nbsp; &nbsp; &nbsp;<Style TargetType="ListViewItem">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EventSetter Event="MouseEnter" Handler="ListViewItem_MouseEnter" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EventSetter Event="MouseLeave" Handler="ListViewItem_MouseLeave" />&nbsp; &nbsp; &nbsp; &nbsp;</Style>&nbsp;</ListView.Resources>在后面的代码中,使用以下代码来确定当前悬停的项目。private ListViewItem _currentItem = null;private void ListViewItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e){&nbsp; &nbsp; var item = sender as ListViewItem;&nbsp; &nbsp; if (!Equals(_currentItem, item))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _currentItem = item;&nbsp; &nbsp; &nbsp; &nbsp; // code to update window&nbsp;&nbsp; &nbsp; }}private void ListViewItem_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){&nbsp; &nbsp; _currentItem = null;}
随时随地看视频慕课网APP
我要回答