一般来说,我想为无法覆盖的属性更改添加逻辑。
具体来说,我已经扩展了一个DataGridView以提供其他功能,但是现在我想在有人更改ReadOnly标志时做某事。我无法覆盖,因为它不是虚拟的或抽象的,即使我可以使System.Windows.Forms.DataGridView不会执行它通常会执行的操作(比如使每个列只读)。如何为此属性更改添加逻辑?
class MyDGV : System.Windows.Forms.DataGridView
{
....
// Cannot override ... This code is totally wrong, but I hope you understand the intent
// Was hoping to do something similar to an overloaded constructor
public bool override ReadOnly
{
get { return base.ReadOnly; }
set
{
MessageBox.Show("You set ReadOnly");
base.ReadOnly = value;
}
}
}
public void MyMethod()
{
MyDGV myDGV = new MyDGV();
// On this property change I want to perform an action, the same action
// anytime a MyDGV changes ReadOnly (like warn the user via MessageBox)
myDGV.ReadOnly = true;
}