DataGridComboBoxColumn

DataGridComboBoxColumn

本文介绍了DataGridComboBoxColumn结合列出<&枚举GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲枚举值的列表绑定到'DataGridComboBoxColumn'。
我已经尝试了很多,但没有真正的作品。

I want to bind a list of enum values to a 'DataGridComboBoxColumn'.I've tried a lot, but nothing really works.

下面是我:

视图模型类:

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
    }

codebehind:

Codebehind:

 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。我做错了什么? THX对您有所帮助。

If I start the project, nothing is displayed in the DataGridComboBoxColumn. What did I do wrong? Thx for you help.

编辑:您可以下载该项目(在VS 2013内置),在这里:

you can download the project (built with VS 2013) here:Download

推荐答案

使用的ObjectDataProvider 中提到的这个例子的。这是结合枚举组合框,因为你不必再手工填写枚举集合中的code中的preferred方式。

Use ObjectDataProvider as mentioned in this example. It's the preferred way of binding enum to combo boxes since you don't have to manually fill enum collection in your code.

声明的ObjectDataProvider 在你的资源:

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

(你必须声明你的局部命名空间核心命名空间:的xmlns:核心=CLR的命名空间:系统;装配= mscorlib程序

然后将其绑定 DataGridComboBoxColumn 来的:

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

修改

既然你已经修改运行您的收藏枚举,看看

Since you have to modify your enum collection in runtime, take a look at this question

在总之,这是 DataGridComboBoxColumn 的一个已知的问题。你必须要改变其风格的元素:

In short, it's a known issue of DataGridComboBoxColumn. You have to alter its element style:

<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>

这篇关于DataGridComboBoxColumn结合列出&LT;&枚举GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 17:34