Search code examples
c#wpfxaml

Get enum description and send to List


I am very new in WPF and I don't know much about this. I have a group dataGrid list with group that makes a dropdown list in my dataGrid. I want to change the enum value to string so I add a description on my enum. But what makes me confused is how to call the description on my enum and set it to my list. Below is my code.

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

        } 

*** And this is my 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>

Solution

  • The easiest and most MVVM friendly way to do this would be to add a read-only property (called "TypeDescription" in the example below) to the User class that gets the description of the enum and then bind to or group by this one:

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