wpf listView中包含button,用于编辑选中的行

  1. 需求:
    对listView 中的数据实现删除选中行
  2. 解决方案
    直接在listview 中添加一列button,用于实现点击时,删除选中行
  3. 代码
    xaml文件
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

        <StackPanel>
            <Button x:Name="loadBtn" Content="加载数据" Click="LoadBtn_Click" Margin="20" Width="200"/>

            <Border BorderBrush="Orange" BorderThickness="5" CornerRadius="6" Padding="4">
            <ListView x:Name="list" ItemsSource="{Binding ShowList}" Height="333" Margin="0.2,0,-0.2,0">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID" Width="50"  DisplayMemberBinding="{Binding Id}"/>
                        <GridViewColumn Header="QrStart" Width="100"   DisplayMemberBinding="{Binding QrStart}" />
                        <GridViewColumn Header="QR" Width="100"   DisplayMemberBinding="{Binding Qr}"/>
                        <GridViewColumn Header="STATE" Width="100"  DisplayMemberBinding="{Binding Res}" />
                        <GridViewColumn Header="Time" Width="100"  DisplayMemberBinding="{Binding Span}" />
                        <GridViewColumn Header="编辑" Width="100" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="删除" x:Name="deleteBtn" Click="DeleteBtn_Click"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                    </GridView>
                </ListView.View>
            </ListView>
        </Border>
        </StackPanel>

    </Grid>
</Window>

后台代码

using System.Windows;
using System.Windows.Controls;

namespace WpfApp2
{
     
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
     
        public MainWindow()
        {
     
            InitializeComponent();

            ///绑定数据
            this.list.ItemsSource = _bindingClass.ShowList;

        }

        private viewmodel.BindingClass _bindingClass = new viewmodel.BindingClass();
        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
     
            //删除选中的某项
            var btn = sender as Button;
            var c = btn.DataContext as viewmodel.CShowTag;
            int index = int.Parse(c.Id);
            this.list.Items.GetItemAt(index); 
            MessageBoxResult boxResult =  MessageBox.Show($"确定删除:id={c.Id},Qr={c.Qr} 吗?","提示", MessageBoxButton.YesNo,MessageBoxImage.Question);
            if (boxResult == MessageBoxResult.Yes)
            {
     
                _bindingClass.ShowList.Remove(c);
                this.list.Items.Refresh();
            }
            return;
        }

        /// 
        /// 加载数据
        /// 
        /// 
        /// 
        private void LoadBtn_Click(object sender, RoutedEventArgs e)
        {
     
            _bindingClass.LoadData();
        }
    }
}

类文件

/*************************************************************************************
  * CLR版本: 4.0.30319.42000
  *  文件名: BindingClass
  *创建时间: 8/31/2020 10:34:33 AM
  *    作者: zhx 
 *文件说明:测试删除功能
 *************************************************************************************/

using System.Collections.ObjectModel;

namespace WpfApp2.viewmodel
{
     
    public class BindingClass
    {
     
        public BindingClass()
        {
     
            ShowList = new ObservableCollection<CShowTag>();
        }

        public ObservableCollection<CShowTag> ShowList
        {
     
            set;
            get;
        }

        public void LoadData()
        {
     
            for (int i = 0; i < 10; i++)
            {
     
                CShowTag tag = new CShowTag()
                {
     
                    Id=(i+1).ToString(),
                    Qr="33333",
                    QrStart="1111",
                    Res="0",
                    Span="33"
                };
                ShowList.Insert(0,tag);
            }
            
        }
    }
}

/*************************************************************************************
  * CLR版本: 4.0.30319.42000
  *  文件名: CShowTag
  *创建时间: 8/31/2020 10:29:08 AM
  *    作者: zhx 
 *文件说明:测试删除功能
 *************************************************************************************/


namespace WpfApp2.viewmodel
{
     
    public class CShowTag
    {
     
        public string Id
        {
     
            get => id;
            set => id = value;
        }
   
        private string id;
      
   
        private string qr;
    
        private string res;

        public string QrStart
        {
     
            get; set;
        }
       

        public string Qr
        {
     
            get => qr;
            set => qr = value;
        }

        public string Res
        {
     
            get => res;
            set => res = value;
        }

        public string Span
        {
     
            get => span;
            set => span = value;
        }

        private string span;
    }
}


你可能感兴趣的:(wpf,学习笔记)