views:

115

answers:

1

In my database I have a 2 columns (Kind and Priority) that are of type int. I also have an enum with the values of Kind and Priority. But when I try to display them into a GridViewColumn, it shows the integers and not the enum values. Do I need a converter Here is the enums:

public enum Kind
{
    None = 0,
    Task = 1,
    Assignment = 2,
    Quiz = 3,
    Test = 4,
    Exam = 5,
    Project = 6,

}
public enum Priority
{
    None = 0,
    Low = 1,
    Medium = 2,
    High = 3,
    Top = 4,
}

And my XAML:

<GridViewColumn x:Name="PriorityColumn" Header="Priority" Width="60" DisplayMemberBinding="{Binding Path=Priority}" />
<GridViewColumn x:Name="KindColumn" Header="Kind" Width="60" DisplayMemberBinding="{Binding Path=Kind}" />
+1  A: 

If the Priority and Kind properties are declared as type int, then yes, you will need a converter. WPF has no way of knowing that the properties declared as integer need to be interpreted as values from the enums.

If the Priority and Kind properties are declared as type Priority and Kind respectively, then it should just work: WPF displays enums by calling ToString() on them which will result in the name of the enum value.

For example:

// As per your question
public enum Kind { ... }
public enum Priority { ... }

// Your database object
public class Job : INotifyPropertyChanged
{
  public Kind Kind { get ... set ... }             // note Kind not int
  public Priority Priority { get ... set ... }     // note Priority not int
}

(When populating a Job object you will of course need to cast the ints coming out of the database to Kind and Priority, or you stick with an int backing field and do the casting in the getter and setter. If you are using a good ORM then it should be able to take care of this for you.)

itowlson
Could you post a sample?
Mohit Deshpande
I am not sure what you need from a sample but I've expanded the answer to show what your database object would want to look like.
itowlson
I thought of using IValueConverter?
Mohit Deshpande
If you really want to keep the business object properties as type int, then you could use an IValueConverter. In that case the implementation of the Convert method is just to cast to int and then to the enum type e.g. `return (Priority)(int)value;`. (You need the cast to int because of boxing.)
itowlson