0

我遇到了 propertyGrid 的问题。

我有一个名为 DummySettings 的类,它映射到 propertyGrid 我有一个属性“名称”

在 propertygrid EG 中键入内容时,我想引发一个“TextChanged”事件

尽管实现了 INotifyPropertyChanged 事件并引发了它,并且尽管在 propertyGrid 上挂钩了我能想到的各种事件,但当 A 文本更改时,这些事件都不会触发。

我错过了显而易见的事情吗?

已编辑

伪代码

   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();
    }
4

1 回答 1

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"
}
于 2013-02-13T21:54:43.637 回答