【问题标题】:Xceed propertygrid not showing DisplayNameXceed propertygrid 不显示 DisplayName
【发布时间】:2015-08-18 08:10:58
【问题描述】:

我在我的项目中使用 Xceed propertygrid,由于某种原因,当我打开属性的下拉列表时,它显示“Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item” 而不是我插入的项目。 我确定这是因为调用了toString() 方法,我只是不知道为什么.. 我看到了这个问题 WPF Xceed PropertyGrid showing "Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item" instead of the real DisplayName ,这正是我的问题,但似乎他没有解决方案。我已经尝试了很多尝试解决方案,但没有奏效。 有什么建议吗?

【问题讨论】:

    标签: c# wpf propertygrid xceed


    【解决方案1】:

    您可以重写 ToString 方法来显示您想要的任何属性,例如,假设我们有以下类作为您的 propertyGrid 控件的 SelectedObject

    public class Company
    {
        [Category("Main")]
        [DisplayName("Name")]
        [Description("Property description")]
        public String Name { get; set; }
        [Category("Main")]
        [DisplayName("Type")]
        [Description("Property description")]
        public String Type { get; set; }
        [Category("Main")]
        [DisplayName("Something")]
        [Description("Property description")]
        public bool Something { get; set; }
        [Category("Main")]
        [DisplayName("Director")]
        [Description("Property description")]
        [ItemsSource(typeof(EmployeList))]
        public Employe Director { get; set; }
    }
    

    集合应该定义如下

     public class EmployeList : IItemsSource
    {
        public Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection GetValues()
        {
            Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection employe = new Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection();
            employe.Add(new Employe()
            {
                Name = "Name1",
                Rank = "Rank1",
                Age=40,
            }); employe.Add(new Employe()
            {
                Name = "Name2",
                Rank = "Rank2",
                Age=40,
            }); employe.Add(new Employe()
            {
                Name = "Name3",
                Rank = "Rank3",
                Age=40,
            });
            return employe;
        }
    }
    

    并且Employe 类应该覆盖Tostring 方法

      public class Employe
    {        
        public String Name { get; set; }
        public String Rank { get; set; }
        public int Age { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
    

    xaml

    <xctk:PropertyGrid  Name="pg"  SelectedObject="{Binding SelectedCompany}" AutoGenerateProperties="True" >                
        </xctk:PropertyGrid>
    

    结果就是你要找的东西

    【讨论】:

    • 感谢您的回答。我已经尝试过这个解决方案,问题是一旦进入ItemCollection,就会构造一个新的Item类并进入集合。所以调用的 ToString 函数仍然是 Item 的函数。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多