1

我有以下字符串类型的属性。

[Category("General")]
[DisplayName("Book Name")]
public string BookName
{ //getter;
  //setter;
}

将包含此属性的对象绑定到propertygrid时,我想提供一个字符串类型列表作为源。

List<string> booksource = new List<string>();

当 Property 是枚举类型时,它会自动填充组合框,我想通过集合实现相同的功能。

编辑:扩展:

enum BookType
    {
        Novel = 0,
        Magazine = 1
    }

    class Class1
    {
        string _bookname = "Book 1";
        BookType _booktype = BookType.Magazine;

        [Category("General")]
        [DisplayName("Book Name")]
        public string BookName
        {
            get { return this._bookname; }
            set { this._bookname = value; }
        }

        [Category("General")]
        [DisplayName("Book Type")]
        public BookType BookType
        {
            get { return this._booktype; }
            set { this._booktype = value; }
        }
    }

     public partial class MainWindow : Window
    {
         public MainWindow()
         {
             InitializeComponent();
             Class1 obj = new Class1();
             this.wpfpropertygrid.SelectedObject = obj;
         }

    }

对于上面的代码,propertygrid 显示一个组合框,其中包含属性 BookType 的项目“Magazine”和“Novel”,以及属性 BookName 的文本框,其中包含文本“Book 1”。我希望属性 BookName 显示为组合框,我可以明确地提供源。我想将列表 {"Book 1","Book 2","Book 3"} 绑定到属性 BookName,以便用户可以选择其中任何一个。

4

1 回答 1

1

迟到总比不到好 ;-)

使用Extended WPF Toolkit 中的 PropertyGrid,您可以这样做:

enum BookType
{
    Novel = 0,
    Magazine = 1
}

public class BookItemsSource : IItemsSource
{
    public ItemCollection GetValues()
    {
        var books = new ItemCollection();
        books.Add("Book 1");
        books.Add("Book 2");
        books.Add("Book 3");
        return books;
    }
}

public class Class1
{
    string _bookname = "Book 1";
    BookType _booktype = BookType.Magazine;

    [Category("General")]
    [DisplayName("Book Name")]
    [ItemsSource(typeof(BookItemsSource))]
    public string BookName
    {
        get { return this._bookname; }
        set { this._bookname = value; }
    }

    [Category("General")]
    [DisplayName("Book Type")]
    public BookType BookType
    {
        get { return this._booktype; }
        set { this._booktype = value; }
    }
}

public partial class MainWindow : Window
{
     public MainWindow()
     {
         InitializeComponent();
         Class1 obj = new Class1();
         this.wpfpropertygrid.SelectedObject = obj;
     }

}
于 2015-02-13T18:45:03.003 回答