无法填充组合框

时间:2012-10-29 19:16:16

标签: c# .net wpf xaml mvvm

我有一个像这样的模型类:

public class AttributesModel
    {
        public SortOrder SortBy { get; set; }

        public enum SortOrder
        {
            Unsorted,
            Ascending,
            Descending
        }

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

一个包含Combobox作为其中一列的DataGrid,如下所示:

<DataGridTemplateColumn Width="Auto" IsReadOnly="False" Header="Order">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=AttributesModel}" DisplayMemberPath="SortBy" SelectedValuePath="SortBy" SelectedValue="{Binding Path=AttributesModel}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

包含DataGrid的类还具有以下构造函数:

DataContext = this;
itemsSource = new ObservableCollection<AttributesModel>(parentDatabaseTable.ListFields.Select(f => new AttributesModel(f)));

出于某种原因,除了组合框之外,我的DataGrid中的所有字段都在填充。请注意,为了简单性和可读性,我没有为模型类中的其他字段或DataGrid中的列包含代码。除组合框列外,它们都成功填充。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

ItemsSource必须是一个集合。您AttributeModel不是收藏品。

如果你想绑定枚举的选项,我过去曾经使用过它:

public class EnumWrapper<T> where T:struct
{
    private List<T> values;

    public List<T> Values
    {
        get { return values; }
        set { values = value; }
    }

    public EnumWrapper()
    {
        // Note: Annoyingly, you can't restrict T to an Enum, so we have to check at runtime!
        Type type = typeof(T);
        if (!type.IsEnum)
        {
            throw new ArgumentException("Type must be an enum");
        }
        Array a = Enum.GetValues(type);
        values = a.Cast<T>().ToList();
    }
}

您可以这样使用:

 EnumWrapper<SortOrder> SortOptions = new EnumWrapper<SortOrder>();

然后,您可以将其公开为属性并将其用作ItemsSource

<ComboBox ItemsSource="{Binding SortOptions}" SelectedValue="{Binding Path=SortBy}"/>

答案 1 :(得分:1)

虽然Matt上面的答案应该在理论上有效,但如果你不想制作包装器,你可以使用这个基于xaml的代码。它需要您的Enums包含在根命名空间中而不是嵌套在类中,但除此之外,您可以在ObjectDataProvider之上创建StaticResource Enum并将其绑定到ComboBox

<UserControl x:Class="TestApplication.DatabaseTable"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:local="clr-namespace:TestApplication"
             mc:Ignorable="d" Width="Auto" Height="Auto">
    <UserControl.Resources>
        <ObjectDataProvider ObjectType="{x:Type sys:Enum}" MethodName="GetValues" x:Key="SortOrderProvider">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:SortOrder" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </UserControl.Resources>
        <DataGrid x:Name="grid" ItemsSource="{Binding ItemsSource}" AutoGenerateColumns="True">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Order" ItemsSource="{Binding Source={StaticResource SortOrderProvider}}" SelectedItemBinding="{Binding SortBy, Mode=TwoWay}"/>
        </DataGrid.Columns>
    </DataGrid>
</UserControl>