赞
踩
这段代码实现的功能
前端代码
<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>
我们还要对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; } } }
效果图如下
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。