在DataGrid中绑定组合框

时间:2012-10-26 18:14:46

标签: c# wpf xaml mvvm datagrid

我已将以下模型类设置为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;
        }
    }

我基本上是尝试使用上面的prpoerties绑定我的DataGrid中的列。除了为组合框列设置的enums之外,它们都有效。

我似乎无法将组合框绑定。这就是我尝试过的:

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

1 个答案:

答案 0 :(得分:2)

ItemsSource期望IEnumerable值,enum不是IEnumerable。试试这个:

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