2010-03-31 67 views
0

属性网格不显示选定对象的新值。.NET 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中。

+0

我无法重现您的问题。你的代码在我的示例中运行良好。如果网格尚未第一次显示,则甚至不需要刷新。也许你应该发布一些我们可以测试的代码。 – 2010-03-31 11:38:20

回答

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

因为你只是没有给出一些代码,这里是一个工作的例子。 只需将一个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)); 
      } 
     } 
    } 
} 
+0

我猜* osss *缺少像我一样的'INotifyPropertyChanged'。 – Wernight 2012-07-03 13:09:34