wpf listview 的右键菜单 点击事件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012434961/article/details/72571570

Mainwindow.xmal

<Window x:Class="vlctesst.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="480" Width="874.254">
   

    <Grid>
        <ListView  Background="AliceBlue" Name="PlayerList" HorizontalAlignment="Left" Height="340" Margin="485,83,0,0" VerticalAlignment="Top" Width="353">
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="时间" Width="80" DisplayMemberBinding="{Binding Path=Time}" ></GridViewColumn>
                    <GridViewColumn Header="播放文件" Width="60" DisplayMemberBinding="{Binding Path=File}" ></GridViewColumn>
                    <GridViewColumn Header="次数" Width="60" DisplayMemberBinding="{Binding Path=Frequency}" ></GridViewColumn>
                </GridView>
            </ListView.View>
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="删除" Click="DelectPlayEvent">
                        <MenuItem.Icon>
                            <Image Source="delect.jpg" Width="20" Height="20"  />
                        </MenuItem.Icon>
                    </MenuItem>
                   
                </ContextMenu>
            </ListView.ContextMenu>
        </ListView>
        <ComboBox  Name="TimeH" HorizontalAlignment="Left" Height="25" Margin="500,23,0,0" VerticalAlignment="Top" Width="42"/>
        <ComboBox  Name="TimeM" HorizontalAlignment="Left" Height="25" Margin="567,23,0,0" VerticalAlignment="Top" Width="42"/>
        <Button Name="fileChoice" Content="videoFile" HorizontalAlignment="Left" Height="25" Margin="629,23,0,0" VerticalAlignment="Top" Width="129" RenderTransformOrigin="0.182,0.428" Click="Button_Click"/>
        <Button Name="OK" Content="OK" HorizontalAlignment="Left" Height="25" Margin="777,23,0,0" VerticalAlignment="Top" Width="61" Click="AddPlayer_Click"/>
    </Grid>
</Window>


右键点击菜单选中的触发事件

MainWindow.xaml.cs 

private void DelectPlayEvent(object sender, RoutedEventArgs e)
{
            int num = this.PlayerList.SelectedIndex; //选中的listview的行
            this.PlayerList.ItemsSource = null;
            if(dataList.Count != 0)
                dataList.RemoveAt(num);   //删除listview的选中的行
            this.PlayerList.ItemsSource = dataList;
}


展开阅读全文

没有更多推荐了,返回首页