简体   繁体   中英

Binding combobox in a DataGrid

I've got the following model class set as the source of my DataGrid :

public class AttributesModel
    {
        public string Field { get; private set; }

        [Display(Name = "Sort Order")]
        public SortOrder SortBy { get; set; }

        [Display(Name = "Group By")]
        public string GroupBy { get; set; }

        [Display(Name = "Having")]
        public string Having { get; set; }

        [Display(Name = "Display Order")]
        public string DisplayOrder { get; set; }

        [Display(Name = "Aggregate By")]
        public Aggregate AggregateBy { get; set; }

        public enum Aggregate
        {
            None,
            Sum,
            Minimum,
            Maximum,
            Average
        }

        public enum SortOrder
        {
            Unsorted,
            Ascending,
            Descending
        }

        public AttributesModel(string field)
        {
            Field = field;
        }
    }

I'm basically trying to bind the columns in my DataGrid, with the prpoerties above. They all work except for the enums which are set up for the combo box columns.

I cannot seem to get the comboboxes to bind. This is what I've tried:

<DataGridComboBoxColumn Width="Auto" CanUserResize="False" CanUserReorder="False" CanUserSort="False" IsReadOnly="False" ItemsSource="{Binding ElementName=AttributesWindow, Path=SortOrder}">
                    <DataGridComboBoxColumn.Header>Order</DataGridComboBoxColumn.Header>
                </DataGridComboBoxColumn>

ItemsSource expects an IEnumerable value, enum is not IEnumerable . Try this:

public IEnumerable<SortOrder> SortOrderList
{
    get { return Enum.GetValues(typeof(SortOrder)).Cast<SortOrder>(); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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