-1

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
angel1108
  • 333
  • 4
  • 22
  • Possible duplicate of [Getting attributes of Enum's value](https://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value) – neelesh bodgal Jun 12 '20 at 05:42
  • Does this answer your question? [Converter to show description of an enum, and convert back to enum value on selecting an item from combo box in wpf](https://stackoverflow.com/questions/20290842/converter-to-show-description-of-an-enum-and-convert-back-to-enum-value-on-sele) – Andy Jun 12 '20 at 10:04

2 Answers2

1

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;
        }
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • okay sir but how can I get it to my list as dropdown sir? I'm sorry but I am really new in WPF sir. I just have a few and basic laerning about this. ^_^ – angel1108 Jun 14 '20 at 15:01
  • What do you mean by "as dropdown"? – mm8 Jun 15 '20 at 13:45
  • As Group, it results in my XAML as dropdown in my editable dataGrid. You're answer works but it is a readonly and it creates also another column in my dataGrid sir. – angel1108 Jun 16 '20 at 04:28
  • That's another issue, isn't it? Please don't ask additional questions in the comments field. Ask a new question if you have another issue. You solve this by either setting the `AutoGenerateColumns` to `false` or handle the `AutoGeneratingColumn` event. But again, this has nothing to do with your original question. – mm8 Jun 16 '20 at 13:43
-1

You can try this trick

as an array

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

or as IEnumerable

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

thank u