C#WPFListView操作item(包括item⼦集中创建listview属性)
这段代码实现的功能
1. listview的item中添加button
2. listview的item中添加图⽚
3. 向item中添加其下的listview
4. 可以对listview进⾏增删改操作
5. 对listview中任意⼀个item的⼦集中的listview中的item对象进⾏增删改操作
前端代码
<Window x:Class="ListviewInItemAddList.MainWindow"
xmlns="schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft/winfx/2006/xaml"
xmlns:d="schemas.microsoft/expression/blend/2008"
xmlns:mc="/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_S electionChanged">
<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>
<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>
/// <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 + " ");
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)
listview控件在哪里{
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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论