简体   繁体   English

有没有办法让枚举显示空间和图像?

[英]Is there any way to make enum display spaces and images?

I am using enum for my group description in listview and I am trying to make it display some more user friendly text but I getting an error which says cannot implicitly convert type string to Mærke我在 listview 中使用 enum 作为我的组描述,我试图让它显示一些更用户友好的文本,但我收到一个错误,提示无法将类型字符串隐式转换为 Mærke

Enum枚举

public enum Mærke { 
 Alfa_Romeo,
 Audi,
 Aston_Martin__________________________________________________________5x114,
BMW,
 Chervolet,
Chrysler,
Citroën,
Daewoo,
Daihatsu,
Dodge,
Ferrari };
public Mærke mærke { get; set; }

Class班级

public class biler
{
    public string billed { get; set; }

    public string Model { get; set; }

    public string Type { get; set; }

    public string Årgang { get; set; }

    public string Krydsmål { get; set; }

    public double ET { get; set; }

    public double centerhul { get; set; }

    public string bolter { get; set; }

    public string hjul { get; set; }
     
    public Mærke mærke { get; set; }

}

List列表

  items.Add(new biler() { billed = "img/Biler/aston martin.png", Model = "DB9", Årgang = "03-", Krydsmål = "5x114.3", ET = 62.5, centerhul = 68.1, bolter = "M14x2", mærke = Mærke.Aston_Martin__________________________________________________________________________________________________5x114 });
 CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView (hjuldata.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("mærke");           
view.GroupDescriptions.Add(groupDescription);

我怎么在这里得到一个图标?

var mærke = Merke.Alfa_Romeo; // example
var withSpaces = mærke.ToString().Replace("_", " ");

This should solve it for you.这应该为您解决。 There's nothing built-in to do that so you COULD write an extension method like such:没有任何内置功能可以做到这一点,因此您可以编写这样的扩展方法:

public static string WithSpaces(this enum theEnum){
  return theEnum.ToString().Replace("_", " ");
}

and then just use that in your code:然后在你的代码中使用它:

var mærke = Mærke.Alfa_Romeo.WithSpaces();

this should do it if that is what you are looking for and as for the picture i dont think headers supports that如果这是您要查找的内容,则应该这样做,至于图片,我认为标题不支持该内容

using System.Reflection;
public static class EnumExtensions
{

    public static string DisplayName(this Enum value)
    {
        FieldInfo field = value.GetType().GetField(value.ToString());

        EnumDisplayNameAttribute attribute
                = Attribute.GetCustomAttribute(field, typeof(EnumDisplayNameAttribute))
                    as EnumDisplayNameAttribute;

        return attribute == null ? value.ToString() : attribute.DisplayName;
    }
}

public class EnumDisplayNameAttribute : Attribute
{
    private string _displayName;
    public string DisplayName
    {
        get { return _displayName; }
        set { _displayName = value; }
    }
}

public enum Mærke 
{
    [EnumDisplayName(DisplayName = "Alfa Romeo 5x114")]
    Alfa_Romeo,

public string mærke { get; set; }
mærke = Mærke.Alfa_Romeo.DisplayName() 

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

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