DataGridComboBoxColumn绑定到List <enum> </enum>

时间:2014-05-08 07:14:35

标签: wpf list binding enumeration datagridcomboboxcolumn

我想将枚举值列表绑定到'DataGridComboBoxColumn'。 我已经尝试了很多,但没有真正有效。

这就是我所拥有的:

视图模型级:

public class ViewModel
    {

        public ViewModel()
        {
            TestCollection= new ObservableCollection<MyEnum>();
            AnyClasses = new ObservableCollection<AnyClass>();

            //... fill AnyClasses with stuff...

            TestCollection.Add(MyEnum.Value1);
            TestCollection.Add(MyEnum.Value2);
            TestCollection.Add(MyEnum.Value3);
            TestCollection.Add(MyEnum.Value4);
            TestCollection.Add(MyEnum.Value5);

        }

        public ObservableCollection<MyEnum> TestCollection { get; set; }
       public ObservableCollection<AnyClass> AnyClasses { get; private set; }

}

我的枚举:

public enum MyEnum
    {
        Value1,
        Value2,
        Value3,
        Value4,
        Value5
    }

代码隐藏:

 public partial class WPFWindow
    {
        private ViewModel Vm { get; set; }

        public WPFWindow() 
        { 
            InitializeComponent(); 
            Vm = new ViewModel(); 
            DataContext = Vm; 
        }
...
    }

最后是XAML:

<DataGrid AutoGenerateColumns="False" Height="289" x:Name="dataGridAnything" ItemsSource="{Binding AnyClasses}" >
  <DataGrid.Columns>
    <DataGridComboBoxColumn Width="200" Header="Optionen" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}, Path=DataContext.TestCollection}" SelectedValuePath="Value"/>
  </DataGrid.Columns>
</DataGrid>

如果我启动项目,DataGridComboBoxColumn中不会显示任何内容。我做错了什么?谢谢你的帮助。

编辑:您可以在此处下载项目(使用VS 2013构建): Download

1 个答案:

答案 0 :(得分:2)

使用this example中提到的ObjectDataProvider。这是将枚举绑定到组合框的首选方法,因为您不必在代码中手动填充枚举集合。

在您的资源中声明ObjectDataProvider

<ObjectDataProvider x:Key="myEnum" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type Type="{x:Type l:MyEnum}"/>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

(您必须声明本地名称空间lcore名称空间:xmlns:core="clr-namespace:System;assembly=mscorlib"

然后将DataGridComboBoxColumn绑定到它:

<DataGridComboBoxColumn Width="200" Header="Optionen" ItemsSource="{Binding Source={StaticResource myEnum}}" SelectedValuePath="Value"/>

修改

由于您必须在运行时修改枚举集合,请查看this question

简而言之,这是DataGridComboBoxColumn的已知问题。你必须改变它的元素风格:

<DataGridComboBoxColumn Width="200" Header="Optionen">
    <DataGridComboBoxColumn.ElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" 
                                    Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, 
                                Path=DataContext.TestCollection}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, 
                                Path=DataContext.TestCollection}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>