2010-11-03 68 views
5

你好,這是我的第一個堆棧溢出問題,所以請原諒我,如果我做任何愚蠢的事情。 那麼我的問題是,我正在關卡編輯器上工作,我想使用PropertyGrid控件來編輯瓷磚/實體等屬性。所以一切正常,值顯示正確,更新時更改槽代碼,但問題我expierencing是我不能改變值,除非它是一個布爾值,我GOOGLE了很多,但我只是找不到解決方案。Winforms PropertyGrid - 屬性不可編輯

這裏是代碼,我定義的屬性:

[Description("Defines the Position on the screen")] 
    public Vector2 screenpos { get; set; } 
    Vector2 WorldPos; 
    [Description("Defines the texture of the selected tile")] 
    public string texture { get; set; } 
    [Description("Defines if the player can collide with this tile")] 
    public bool IsCollidable { get; set; } 
    [Description("Defines on what layer this tile is drawn (1-3)")] 
    public int Layer { get; set; } 
    [Description("Shows if the tile is currently visible on the screen")] 
    public bool OnScreen { get; private set; } 

我可以編輯IsCollidable,如果我從屏幕上的集中刪除私人我可以編輯太多,但我不能編輯別的,噢,我會appriciate如果你可以說你的答案有點簡單我沒有那麼多expierenced程序員,在此先感謝。

回答

0

我修好了,我有this.KeyPreview = true;在我的Form1構造函數中,通過刪除我可以修復問題。 感謝您的幫助!

1

您需要爲您的Vector2類創建一個自定義用戶界面類型編輯器。

Here's a tutorial

5

具有公共屬性(即讀取和寫入)的大多數標準類型應該是可編輯的。

如果Vector2是相當簡單的,你只是希望它在PropertyGrid擴大,則:

[Description("Defines the Position on the screen")] 
[TypeConverter(typeof(ExpandableObjectConverter))] 
public Vector2 screenpos { get; set; } 

如果Vector2是你自己的代碼,那麼你也可以裝飾Vector2本身並會被應用所有屬性:

[TypeConverter(typeof(ExpandableObjectConverter))] 
public {class|struct} Vector2 {...} 

還有一招,做到這一點的類型你的c之外 ONTROL;在應用程序啓動,運行:

TypeDescriptor.AddAttributes(typeof(Vector2), 
    new TypeConverterAttribute(typeof(ExpandableObjectConverter))); 
0

我正在expierencing的問題是,除非它是一個布爾

我沒有看到你的代碼的任何這就解釋了這一點,我不能改變的值。你不需要做任何特殊,使intstring性能在PropertyGrid中編輯,通過這個小例子所證明:

using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication13 
{ 
    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new MainForm()); 
     } 
    } 

    public class Test 
    { 
     public string StringProperty { get; set; } 
     public int IntProperty { get; set; } 
     public bool BoolProperty { get; set; } 
    } 

    class MainForm : Form 
    { 
     public MainForm() 
     { 
     var propertyGrid = new PropertyGrid() { Dock = DockStyle.Fill }; 
     this.Controls.Add(propertyGrid); 
     propertyGrid.SelectedObject = new Test(); 
     } 
    } 
} 

我可以用上面的代碼編輯所有三個屬性。你怎麼確定你「不能改變價值」?你究竟看到了什麼?

+0

我看到他們像一個普通的屬性網格,不灰掉任何東西,我可以移動文本光標和東西,但如果我鍵入(字母或數字)沒有任何反應。 – 2010-11-03 13:51:46