views:

133

answers:

2

Property grid do not show new value of selected object.

For example:

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

The property in property grid is still "1"; It is changing only if you click on this property.

Or like that:

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

but in this case focus will be changed to PropertyGrid.

+1  A: 

As I told you in my comment, your code is not enough to understand your issue. Presented like this it should work. Here is mine that works well:

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

The call to Refresh() actually rebuilds the grid. Remove it and you will see the change only when you click the property.

Nicolas Cadilhac
A: 

Cause you just not gave some code, here is a working example. Just put a Button and a PropertyGrid onto a form.

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));
            }
        }
    }
}
Oliver