2012-10-29 44 views
0

我有一个模型类,像这样:麻烦填充一个组合框

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

     public enum SortOrder 
     { 
      Unsorted, 
      Ascending, 
      Descending 
     } 

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

,且含有组合框为一列,像这样一个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中的列。他们都成功地填充,除了组合框列。有任何想法吗?

+0

所以它看起来像你绑定'ComboBox'属性'AttributesModel',*不是'IEnumerable',是吗? – McGarnagle

+0

实际上包含在其中的枚举。我也有其他的领域,它没有任何问题。 –

回答

1

虽然上述马特的答案应该在理论工作,但我如果你不想做一个包装,你可以使用这个基于xaml的代码。它需要将Enums包含在根名称空间中,而不是嵌套在类中,但除此之外,只需在Enum之上创建一个ObjectDataProviderStaticResource并将其绑定到您的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> 
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}"/> 
+0

我为ComboBox设置的属性如何?这些有效吗? –

+0

@DotNET:假设DataContext已经是你的'AttributesModel'类的一个实例并且'SortOptions'是'EnumWrapper ' –

+0

@MattBurland类型的一个属性,我认为你的EnumWrapper构造函数代码有一个错字它。它没有采用任何Type参数,但是你的例子将它传入。我也猜测你的意图是尽可能地写出“令人讨厌的是,你不能限制T ......”:) +1。 – Maverik