当前位置:   article > 正文

C# WPF ListView 操作 item(包括item子集中创建listview属性)_wpf listviewitem

wpf listviewitem

这段代码实现的功能

  1. listview的item中添加button
  2. listview的item中添加图片
  3. 向item中添加其下的listview
  4. 可以对listview进行增删改操作
  5. 对listview中任意一个item的子集中的listview中的item对象进行增删改操作

前端代码

<Window x:Class="ListviewInItemAddList.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:ListviewInItemAddList"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded_1">
    <Grid>
        <ListView Name="list1" HorizontalAlignment="Left" Height="306" Margin="10,10,0,0" VerticalAlignment="Top" Width="401" SelectionChanged="list1_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="列1" Width="50" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Header="列2" Width="50" DisplayMemberBinding="{Binding Path=Id}"/>
                    <GridViewColumn Header="列3" Width="50" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="30" Content="删除" Click="Button_Click1" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="列4" Width="50" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="30" Content="增加" Click="Button_Click" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="列5" Width="50" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="30" Content="修改" Click="Button_Click2"  />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="图片" Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image  Source ="{Binding Path=MyImage}"  Height="30"></Image>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <ListView x:Name="list2" HorizontalAlignment="Left" Height="306" Margin="416,10,0,0" VerticalAlignment="Top" Width="307" ItemsSource="{Binding .}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="添加音乐" DisplayMemberBinding="{Binding Path=Name2}"/>
                    <GridViewColumn Header=" " DisplayMemberBinding="{Binding Path=Id2}"/>
                    <GridViewColumn  Width="50" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="30" Content="删除" Click="list2_Click" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn  Width="50" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="30" Content="修改" Click="list2_Click1" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn  Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image  Source ="{Binding Path=MyImage2}"  Height="30"></Image>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

我们还要对listview这个控件进行设置,下面的操作,要先选定listview控件的基础上点击右键。
在这里插入图片描述

C#代码
C#代码我是直接拷贝过来的直接可以运行的;里面写了注释很详细,不过都是在自己理解的很多地方不透彻,你们可以自己运行一下看看效果。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListviewInItemAddList
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        //_maillist中存放的都是class1的构造函数类型(相当于一个列表);
        private static ObservableCollection<Class1> _maillist = new ObservableCollection<Class1>();
   
        //ListView 的item中要添加图片需要先创建一个图片类,用来指定图片来源;
        Image image = new Image();

        //定义一个静态变量用来存储当前button所属的是那一条item,在增加事件中给其赋值;
        private static Class1 ssss = new Class1();
        public MainWindow()
    {
        InitializeComponent();

        image.Source = new BitmapImage(new Uri("E:/cc.png"));
    }

    private void list1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
            //emp 当前选中的item对象,它的类型的Class1 类型
             Class1 emp = list1.SelectedItem as Class1;
            if (emp != null && emp is Class1)
            {
                //设置当前item对象下的列表内容
                list2.ItemsSource = emp.ListModel2;
            }
     }
        //Liast1中增加
        private void Button_Click(object sender, RoutedEventArgs e)
        {
           
            var btn = sender as Button; //btn 是一个button对象

            var c = btn.DataContext as Class1; //c 表示 btn所属的list1 中的那一条itme,其类型的Class1类型;

            ssss = c;
           
            if (c != null && c is Class1)
        {
                for (int i = 0; i < 7; i++)
                {
                    Class2 _listmodel = new Class2(name: "sz"+i, id: "2z"+i, myImage: image.Source);//Class2也是一个构造函数类型和Class1一样,用来当做list1的item中listView中的内容;

                    c.ListModel2.Add(_listmodel);//添加list1中ListModel2属性的内容(就是添加到当前选中的list1中那个item中)

                    DataContext = _listmodel;//将数据绑定到当前上下文中
                }
               list2.ItemsSource = c.ListModel2;//把数据添加到第二个listview中(list1中item中所属的内容);
            }
        
        }
        //Liast1中修改
        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;

            var c = btn.DataContext as Class1;

            c.Id = "zhou";//我只是将当前list1中的Id进行了修改,来说明数据是修改成功的;

            MessageBox.Show(c.Id + "  " + c.Name + "  ");
        }
        //Liast1中删除
        private void Button_Click1(object sender, RoutedEventArgs e)
        {
        var btn = sender as Button;
        var c = btn.DataContext as Class1;
        //Class1 emp = list1.SelectedItem as Class1;
        if (c != null && c is Class1)
        {
             c.ListModel2.Clear();//先清除list2中的数据

                _maillist.Remove(c);//在清除list1中的数据

            }

        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            //在窗体初始化的时候就添加了list1中的数据
            list1.ItemsSource = null;

            for (int i = 0; i < 7; i++)
            {
                ObservableCollection<Class2> _listmodel = new ObservableCollection<Class2>();//_listmodel一定要在添加list1中数据的时候和list1中的数据一起创建出来,它们数据是绑定的;

                Class1 k = new Class1(name: "s" + i, id: "2" + i, myImage: image.Source, listmodel: _listmodel);

                DataContext = k;

                _maillist.Add(k);
            }
            list1.ItemsSource = _maillist;
        }
        //修改list2
        private void list2_Click1(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;

            var c = btn.DataContext as Class2;
            c.Id2 = "zhou";
            MessageBox.Show(c.Id2 + "  " + c.Name2 + "  ");
        }
        //删除list2
        private void list2_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            
            var c = btn.DataContext as Class2;
            foreach(var item in _maillist)
            {
                if (ssss == item)//为了判断你当前点击添加的时候是属于list1中的那一条item
                {
                    ssss.ListModel2.Remove(c);//删除的是list1中ListModel2属性中的你点击要删除的list2中的那个item数据(有点绕);
                }
            }
        }
    }
    class Class1 : INotifyPropertyChanged
{
    private string _name;
    private string _id;
    private ImageSource _myImage;
    private ObservableCollection<Class2> _listmodel2;
    public event PropertyChangedEventHandler PropertyChanged;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            if (this.PropertyChanged != null)//激发事件,参数为Age属性    
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }

    public string Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
            if (this.PropertyChanged != null)//激发事件,参数为Age属性    
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Id"));
            }
        }
    }

    public ImageSource MyImage
    {
        get
        {
            return _myImage;
        }
        set
        {
            _myImage = value;
            if (this.PropertyChanged != null)//激发事件,参数为Age属性    
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MyImage"));
            }
        }
    }
        public ObservableCollection<Class2> ListModel2
        {
            get
            {
                return _listmodel2;
            }
            set
            {
                _listmodel2 = value;
                if (this.PropertyChanged != null)//激发事件,参数为Age属性    
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ListModel2"));
                }
            }
        }
        public Class1() { }
    public Class1(string name, string id, ImageSource myImage , ObservableCollection<Class2> listmodel)
    {
        this._name = name;
        this._id = id;
        this._myImage = myImage;
        this._listmodel2 = listmodel;
    }
  }
    class Class2 : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _name2;
        private string _id2;
        private ImageSource _myImage2;

        public string Name2
        {
            get
            {
                return _name2;
            }
            set
            {
                _name2 = value;
                if (this.PropertyChanged != null)//激发事件,参数为Age属性    
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name2"));
                }
            }
        }
        public string Id2
        {
            get
            {
                return _id2;
            }
            set
            {
                _id2 = value;
                if (this.PropertyChanged != null)//激发事件,参数为Age属性    
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Id2"));
                }
            }
        }
        public ImageSource MyImage2
        {
            get
            {
                return _myImage2;
            }
            set
            {
                _myImage2 = value;
                if (this.PropertyChanged != null)//激发事件,参数为Age属性    
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MyImage2"));
                }
            }
        }
        public Class2() { }
        public Class2(string name, string id, ImageSource myImage)
        {
            this._name2 = name;
            this._id2 = id;
            this._myImage2 = myImage;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282

效果图如下

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/537706
推荐阅读
相关标签
  

闽ICP备14008679号