简体   繁体   中英

Provide a Collection as source to property in WPF Propertygrid

I have the following property of type string.

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

When binding an object containing this property to propertygrid , I would like to provide a list of type string as source.

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

When Property is of type enum, it automatically populates combobox, I want to acheive same functionality through collection.

Edit : Expanded:

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

    }

For the above code, the propertygrid displays a combobox with items "Magazine" and "Novel" for property BookType and a textbox with text "Book 1" for property BookName. I want the property BookName displayed as combobox to which i can explicitly provide a source. I would like to bind a list {"Book 1","Book 2","Book 3"} to property BookName, so that the user can select any one of them.

Better late than never ;-)

With PropertyGrid from Extended WPF Toolkit you can do this that way:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM