我遇到了propertyGrid问题。
我有一个名为DummySettings的类,它被映射到propertyGrid 我有一个属性“名称”
在propertygrid中键入内容时,我想在Name属性中引发一个“TextChanged”
的事件尽管实现了INotifyPropertyChanged事件并提升了它,尽管挂钩了我可以在propertyGrid上看到的各种事件 当文本发生变化时,这些事件都不会触发。
我错过了明显的吗?
Psuedo Code
public class DummySettings : INotifyPropertyChanged
{
private string name;
[DisplayName("Name")]
public string Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
//UserControl
public partial class DummyControl : UserControl
{
private DummySettings settings;
///Constructor
public DummyControl()
{
InitializeComponent();
settings = new DummySettings();
propertyGrid1.SelectedObject = settings;
settings.PropertyChanged += OnDummyPropertyChanged;
//All the events I have hooked up but not firing when text is changed
private void OnDummyPropertyChanged(object sender, PropertyChangedEventArgs e)
{
btnToEnable.Enabled = HasName();
}
private void propertyGrid1_Leave(object sender, EventArgs e)
{
btnToEnable.Enabled = HasName();
}
private void propertyGrid1_Validating(object sender, CancelEventArgs e)
{
btnToEnable.Enabled = HasName();
}
private void propertyGrid1_SelectedObjectsChanged(object sender, EventArgs e)
{
btnToEnable.Enabled = HasName();
}
private void propertyGrid1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
btnToEnable.Enabled = HasName();
}
private bool HasName()
{
return settings.Name.IsNotNullOrEmpty();
}
答案 0 :(得分:1)
您可以使用PropertyValueChanged事件来获知PropertyGrid中更改的所选对象属性的通知。因此,当您将Name
属性值从“Foo”更改为“Bar”时,将引发此事件:
void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
// previous value is e.OldValue, "Foo"
// property name is e.ChangedItem.Label, "Name"
// new value is e.ChangedItem.Value, "Bar"
}