简体   繁体   English

Xceed propertygrid不显示DisplayName

[英]Xceed propertygrid not showing DisplayName

I am using Xceed propertygrid in my project, and for some reason when I open the dropdown of the property it is showing "Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item" instead of the items i inserted. 我在项目中使用Xceed propertygrid,由于某种原因,当我打开该属性的下拉菜单时,它显示的是“ Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item”,而不是我插入的项目。 I am sure it is because the toString() method is called, i just can't figure out why.. I saw this question WPF Xceed PropertyGrid showing "Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item" instead of the real DisplayName , this is exactly my problem but it doesn't seem that he got a solution. 我相信这是因为toString()方法被调用时,我只是想不通为什么..我看到这个问题WPF Xceed PropertyGrid中显示“Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item”,而不是真正的显示名称的 ,这正是我的问题,但似乎他没有找到解决方案。 I have tried many attempts solution but non worked. 我尝试了许多尝试解决方案,但没有用。 Any suggestions? 有什么建议么?

You can override the ToString method to show whatever property you wanted, for example lets say we have the following class as a SelectedObject for your propertyGrid control: 您可以重写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; }
}

the collection should be defined as follow 集合应定义如下

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

and the Employe class should override the Tostring method 并且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 a

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

and the result is what you are looking for 结果就是您想要的

输出

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

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