gpt4 book ai didi

c# - 在 BindingList 的 ItemChanging 事件中获取 Deleted Item

转载 作者:IT王子 更新时间:2023-10-29 04:26:17 24 4
gpt4 key购买 nike

我在我的应用程序中使用绑定(bind)列表以及 ItemChanged 事件。

有什么方法可以让我知道 ItemChanged 事件中属性的先前值。目前,我正在添加一个名为“OldValue”的单独属性来实现此目的。

有没有办法知道项目更改事件中已删除的项目。我无法找到任何方法来知道哪个项目已从列表中删除。

最佳答案

如果我没理解错的话,你想获取有关已从绑定(bind)列表中删除的项目的信息。

我认为最简单的方法是创建您自己的绑定(bind)列表,该绑定(bind)列表派生自绑定(bind)列表。

在内部,您将覆盖 RemoveItem 方法,因此在从绑定(bind)列表中删除项目之前,您将能够触发包含将要删除的项目的事件。

public class myBindingList<myInt> : BindingList<myInt>
{
protected override void RemoveItem(int itemIndex)
{
//itemIndex = index of item which is going to be removed
//get item from binding list at itemIndex position
myInt deletedItem = this.Items[itemIndex];

if (BeforeRemove != null)
{
//raise event containing item which is going to be removed
BeforeRemove(deletedItem);
}

//remove item from list
base.RemoveItem(itemIndex);
}

public delegate void myIntDelegate(myInt deletedItem);
public event myIntDelegate BeforeRemove;
}

为了示例,我创建了实现 INotifyPropertyChanged 的​​类型 myInt - 接口(interface)只是为了在从绑定(bind)列表中添加/删除元素后刷新 dataGridView。

public class myInt : INotifyPropertyChanged
{
public myInt(int myIntVal)
{
myIntProp = myIntVal;
}
private int iMyInt;
public int myIntProp {
get
{
return iMyInt;
}
set
{
iMyInt = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("myIntProp"));
}
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

我正在使用 int 初始化绑定(bind)列表(准确地说是 myInts),然后我将绑定(bind)列表绑定(bind)到 dataGridView(用于演示目的)并订阅我的 BeforeRemove 事件。

bindingList = new myBindingList<myInt>();
bindingList.Add(new myInt(8));
bindingList.Add(new myInt(9));
bindingList.Add(new myInt(11));
bindingList.Add(new myInt(12));

dataGridView1.DataSource = bindingList;
bindingList.BeforeRemove += bindingList_BeforeRemove;

如果引发了 BeforeRemove 事件,我有已删除的项目

void bindingList_BeforeRemove(Form1.myInt deletedItem)
{
MessageBox.Show("You've just deleted item with value " + deletedItem.myIntProp.ToString());
}

下面是整个示例代码(在表单上放置 3 个按钮和 dataGridView)- 按钮 1 初始化绑定(bind)列表,按钮 2 将项目添加到列表,按钮 3 从投标列表中删除项目

before delete

after delete

deleted

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bindinglist
{
public partial class Form1 : Form
{
myBindingList<myInt> bindingList;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
bindingList = new myBindingList<myInt>();
bindingList.Add(new myInt(8));
bindingList.Add(new myInt(9));
bindingList.Add(new myInt(11));
bindingList.Add(new myInt(12));

dataGridView1.DataSource = bindingList;
bindingList.BeforeRemove += bindingList_BeforeRemove;
}

void bindingList_BeforeRemove(Form1.myInt deletedItem)
{
MessageBox.Show("You've just deleted item with value " + deletedItem.myIntProp.ToString());
}

private void button2_Click(object sender, EventArgs e)
{
bindingList.Add(new myInt(13));
}

private void button3_Click(object sender, EventArgs e)
{
bindingList.RemoveAt(dataGridView1.SelectedRows[0].Index);
}

public class myInt : INotifyPropertyChanged
{
public myInt(int myIntVal)
{
myIntProp = myIntVal;
}
private int iMyInt;
public int myIntProp {
get
{
return iMyInt;
}
set
{
iMyInt = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("myIntProp"));
}
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

public class myBindingList<myInt> : BindingList<myInt>
{
protected override void RemoveItem(int itemIndex)
{
myInt deletedItem = this.Items[itemIndex];

if (BeforeRemove != null)
{
BeforeRemove(deletedItem);
}

base.RemoveItem(itemIndex);
}

public delegate void myIntDelegate(myInt deletedItem);
public event myIntDelegate BeforeRemove;
}
}
}

回复评论

“问题的另一部分是 => 有没有办法知道列表中更改的项目的旧值?在 ListChangedEvent 中不共享任何内容”

要查看项目的旧值,您可以覆盖 SetItem 方法

protected override void SetItem(int index, myInt item)
{
//here we still have old value at index
myInt oldMyInt = this.Items[index];
//new value
myInt newMyInt = item;

if (myIntOldNew != null)
{
//raise event
myIntOldNew(oldMyInt, newMyInt);
}

//update item at index position
base.SetItem(index, item);
}

当指定索引处的对象发生更改时触发,如下所示

bindingList[dataGridView1.SelectedRows[0].Index] = new myInt(new Random().Next());

棘手的部分是,如果您尝试直接修改项目的属性

bindingList[dataGridView1.SelectedRows[0].Index].myIntProp = new Random().Next();

SetItem 不会触发,必须替换整个对象。

所以我们需要另一个委托(delegate)和事件来处理这个

public delegate void myIntDelegateChanged(myInt oldItem, myInt newItem);
public event myIntDelegateChanged myIntOldNew;

然后我们可以订阅这个

bindingList.myIntOldNew += bindingList_myIntOldNew;

并处理它

void bindingList_myIntOldNew(Form1.myInt oldItem, Form1.myInt newItem)
{
MessageBox.Show("You've just CHANGED item with value " + oldItem.myIntProp.ToString() + " to " + newItem.myIntProp.ToString());
}

before event raised changed

更新代码(需要 4 个按钮,第 4 个修改所选项目)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bindinglist
{
public partial class Form1 : Form
{
myBindingList<myInt> bindingList;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
bindingList = new myBindingList<myInt>();
bindingList.Add(new myInt(8));
bindingList.Add(new myInt(9));
bindingList.Add(new myInt(11));
bindingList.Add(new myInt(12));

dataGridView1.DataSource = bindingList;
bindingList.BeforeRemove += bindingList_BeforeRemove;
bindingList.myIntOldNew += bindingList_myIntOldNew;
}

void bindingList_myIntOldNew(Form1.myInt oldItem, Form1.myInt newItem)
{
MessageBox.Show("You've just CHANGED item with value " + oldItem.myIntProp.ToString() + " to " + newItem.myIntProp.ToString());
}

void bindingList_BeforeRemove(Form1.myInt deletedItem)
{
MessageBox.Show("You've just deleted item with value " + deletedItem.myIntProp.ToString());
}

private void button2_Click(object sender, EventArgs e)
{
bindingList.Add(new myInt(13));
}

private void button3_Click(object sender, EventArgs e)
{
bindingList.RemoveAt(dataGridView1.SelectedRows[0].Index);
}

public class myInt : INotifyPropertyChanged
{
public myInt(int myIntVal)
{
myIntProp = myIntVal;
}
private int iMyInt;
public int myIntProp {
get
{
return iMyInt;
}
set
{
iMyInt = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("myIntProp"));
}
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

public class myBindingList<myInt> : BindingList<myInt>
{
protected override void SetItem(int index, myInt item)
{
myInt oldMyInt = this.Items[index];
myInt newMyInt = item;

if (myIntOldNew != null)
{
myIntOldNew(oldMyInt, newMyInt);
}

base.SetItem(index, item);
}

protected override void RemoveItem(int itemIndex)
{
myInt deletedItem = this.Items[itemIndex];

if (BeforeRemove != null)
{
BeforeRemove(deletedItem);
}

base.RemoveItem(itemIndex);
}

public delegate void myIntDelegateChanged(myInt oldItem, myInt newItem);
public event myIntDelegateChanged myIntOldNew;

public delegate void myIntDelegate(myInt deletedItem);
public event myIntDelegate BeforeRemove;
}

private void button4_Click(object sender, EventArgs e)
{
bindingList[dataGridView1.SelectedRows[0].Index] = new myInt(new Random().Next());
}
}
}

关于c# - 在 BindingList 的 ItemChanging 事件中获取 Deleted Item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23339233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com