繁体   English   中英

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

[英]Provide a Collection as source to property in WPF Propertygrid

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

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

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

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

当Property类型为enum时,它将自动填充组合框,我想通过集合实现相同的功能。

编辑 :展开:

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,以便用户可以选择其中的任何一个。

迟到总比不到好 ;-)

使用扩展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