获取枚举描述并发送到列表



我对WPF很陌生,对此了解不多。我有一个组dataGrid列表,其中包含在我的dataGrid中制作下拉列表的组。我想将enum值更改为字符串,因此我在enum上添加了描述。但让我感到困惑的是,如何在enum上调用描述并将其设置为我的列表。下面是我的代码。

C# ***

public ProcessData()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Description = "Operating pressure", Unit = 1, Type = Group.kg_kmol, Case1 = " " });
items.Add(new User() { Description = "Operating Temperature", Unit = 2, Type = Group.DD });
items.Add(new User() { Description = "Gas flow rate", Unit = 3, Type = Group.DD });
items.Add(new User() { Description = "Gas molecular weight", Unit = 4, Type = Group.Unit_DD });
items.Add(new User() { Description = "Gas Desnsity", Unit = 4, Type = Group.Unit_DD });
items.Add(new User() { Description = "HC Liquid flow rate", Unit = 4, Type = Group.Unit_DD });
items.Add(new User() { Description = "Water flow rate", Unit = 5, Type = Group.Unit_DD });
items.Add(new User() { Description = "Sand Flow rate", Unit = 6, Type = Group.Unit_DD });
lvUsers2.ItemsSource = items;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers2.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Unit");
view.GroupDescriptions.Add(groupDescription);
}
public enum Group {
[Description("this is a string 1")]
kg_kmol,
[Description("this is a string 2")]
DD,
[Description("this is a string 3")]
Unit_DD,
SomeValue
};
public class User
{
public string Description { get; set; }
public int Unit { get; set; }
public Group Type { get; set; }
public string Case1 { get; set; }
} 

这是我的 XAML ***

<Grid>
<DataGrid  Name="lvUsers2" ItemsSource="{Binding GroupedCustomers}" >
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<TextBlock  Text="yeah" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Style.Resources>
<ControlTemplate x:Key="MultiItemGroupTemplate" TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
<ControlTemplate x:Key="SingleItemGroupTemplate" TargetType="{x:Type GroupItem}">
<ItemsPresenter />
</ControlTemplate>
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding ItemCount}" Value="1">
<Setter Property="Template" Value="{StaticResource SingleItemGroupTemplate}">
</Setter>
</DataTrigger>
</Style.Triggers>
<Setter Property="Template" Value="{StaticResource MultiItemGroupTemplate}"/>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>

执行此操作的最简单且对 MVVM 最友好的方法是将只读属性(在下面的示例中称为"TypeDescription"(添加到获取枚举描述的User类,然后绑定到或按此类分组:

public class User
{
public string Description { get; set; }
public int Unit { get; set; }
public Group Type { get; set; }
public string Case1 { get; set; }
public string TypeDescription
{
get
{
string stringValue = Type.ToString();
FieldInfo fi = typeof(Group).GetField(Type.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
return attributes != null && attributes.Length > 0 ? attributes[0].Description : stringValue;
}
}
}

你可以试试这个技巧

作为数组

var gh = Enum.GetValues (typeof(Group))

或作为 IEnumerable

IEnumerable <Group> gh = Enum.GetValues ​(typeof(Group)).Cast<Group>();

谢谢

最新更新