.NET Propertygrid刷新麻烦

时间:2010-03-31 10:46:14

标签: c# propertygrid

属性网格不显示所选对象的新值。

例如:

o.val = "1";
pg.SelectedObject = o;
o.val = "2";
pg.Refresh();

属性网格中的属性仍为“1”; 只有在您点击此属性时才会更改。

或者那样:

o.val = "1";
pg.SelectedObject = o;
o.val = "2";
pg.SelectedObject = o;

但在这种情况下,焦点将更改为PropertyGrid。

2 个答案:

答案 0 :(得分:1)

正如我在评论中告诉你的那样,你的代码不足以理解你的问题。像这样呈现它应该工作。这是我的运作良好:

public class Target
{
    private int _myInt = 1;
    public int MyInt { set; get; }

    public static Target target = new Target();
}

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();

        Button button = new Button()
        {
            Text = "Click me",
            Dock = DockStyle.Bottom
        };

        Form form = new Form
        {
            Controls = {
                new PropertyGrid {
                    SelectedObject = Target.target,
                    Dock = DockStyle.Fill,
                },
                button
            }
        };

        button.Click += new EventHandler(button_Click);
        Application.Run(form);
    }

    static void button_Click(object sender, EventArgs e)
    {
        Target.target.MyInt = 2;
        Form form = Form.ActiveForm;
        (form.Controls[0] as PropertyGrid).Refresh();
    }
}

对Refresh()的调用实际上重建了网格。删除它,只有在单击属性时才会看到更改。

答案 1 :(得分:1)

因为你没有给出一些代码,这是一个有效的例子。 只需将Button和PropertyGrid放在表单上即可。

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class FormMain : Form
    {
        Random rand;
        MyObject obj;

        public FormMain()
        {
            InitializeComponent();

            rand = new Random();
            obj = new MyObject();

            propertyGrid1.SelectedObject = obj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            obj.MyValue = rand.Next();
            obj.IsEnabled = !obj.IsEnabled;
            obj.MyText = DateTime.Now.ToString();

            propertyGrid1.Refresh();
        }
    }

    public class MyObject : INotifyPropertyChanged
    {
        private int _MyValue;
        public int MyValue
        {
            get
            {
                return _MyValue;
            }
            set
            {
                _MyValue = value;
                NotifyPropertyChanged("MyValue");
            }
        }

        private string _MyText;
        public string MyText
        {
            get
            {
                return _MyText;
            }
            set
            {
                _MyText = value;
                NotifyPropertyChanged("MyText");
            }
        }

        private bool _IsEnabled;
        public bool IsEnabled
        {
            get
            {
                return _IsEnabled;
            }
            set
            {
                _IsEnabled = value;
                NotifyPropertyChanged("IsEnabled");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}