简体   繁体   English

提供一个Collection作为WPF Propertygrid中财产的来源

[英]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. 将包含此属性的对象绑定到propertygrid时 ,我想提供一个字符串类型列表作为源。

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

When Property is of type enum, it automatically populates combobox, I want to acheive same functionality through collection. 当Property类型为enum时,它将自动填充组合框,我想通过集合实现相同的功能。

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. 对于上面的代码,propertygrid将为属性BookType显示一个带有项目“ Magazine”和“ Novel”的组合框,并为属性BookName显示一个文本为“ Book 1”的文本框。 I want the property BookName displayed as combobox to which i can explicitly provide a source. 我希望属性BookName显示为组合框,我可以向其明确提供源。 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. 我想将列表{“ Book 1”,“ Book 2”,“ Book 3”}绑定到属性BookName,以便用户可以选择其中的任何一个。

Better late than never ;-) 迟到总比不到好 ;-)

With PropertyGrid from Extended WPF Toolkit you can do this that way: 使用扩展WPF工具包中的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;
     }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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