绑定 DataGridComboBoxColumn [英] Binding DataGridComboBoxColumn

查看:42
本文介绍了绑定 DataGridComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 T 的 ObservableCollection 绑定到 DataGrid 的 DataGridComboBoxColumn.
DataGrid 定义是:

I am trying to bind ObservableCollection of T to DataGridComboBoxColumn of DataGrid.
DataGrid definition is :

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Model, IsAsync=True}">

     <DataGrid.Columns>
         <DataGridTextColumn  Header="Column Entry"  IsReadOnly="True" Binding="{Binding ColumnName}"/>
         <DataGridComboBoxColumn Header="Road Type" ItemsSource="{Binding RoadTypes}"/>
    </DataGrid.Columns>

</DataGrid>

这是视图模型和模型

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var viewModel = new ViewModel();
        DataContext = viewModel;
    }
}

public class ViewModel : ViewModelBase
{
    private ObservableCollection<Model> _model;

    public ViewModel()
    {
        var list = new List<Model>();
        var roadTypes = new ObservableCollection<RoadType>
                            {
                                new RoadType
                                    {
                                        Code = 1,
                                        Id = 1,
                                        Name = "Name1"
                                    },
                                new RoadType
                                    {
                                        Code = 1,
                                        Id = 1,
                                        Name = "Name1"
                                    }
                            };

        Model = new ObservableCollection<Model>
                    {
                        new Model
                            {
                                ColumnName = "Col1",
                                RoadTypes = roadTypes
                            },
                        new Model
                            {
                                ColumnName = "Col1",
                                RoadTypes = roadTypes
                            }
                    };
    }

    public ObservableCollection<Model> Model
    {
        get { return _model; }
        set
        {
            _model = value;
            RaisePropertyChanged(() => Model);
        }
    }
}

public class RoadType
{
    public int Id { get; set; }
    public int Code { get; set; }

    public string Name { get; set; }
}

public class Model : ObservableObject
{
    private ObservableCollection<RoadType> _roadTypes;
    public string ColumnName { get; set; }

    public ObservableCollection<RoadType> RoadTypes
    {
        get { return _roadTypes; }
        set
        {
            _roadTypes = value;
            RaisePropertyChanged(() => RoadTypes);
        }
    }
}

DataGrid 也显示文本列,但不显示 ComboBox 值.
怎么了?

DataGrid displays text column as well but it doesn't display ComboBox values.
What's wrong?

推荐答案

由于 RoadTypes 不是一个简单的字符串列表,您需要告诉组合框它需要在 ComboBox 中显示什么属性.尝试添加

Since RoadTypes isn't a simple list of strings you need to tell your combobox what property it needs to display in the ComboBox. Try adding

DisplayMemberPath="Name" 

到您的组合框声明

--

更新:

好的,这是 WPF 数据网格的一个已知功能".问题是 DataGridComboBox 没有 DataGrid 的 DataContext.我将 ComboBox 的绑定修改为如下所示:

Okay, this is a known "feature" with WPF datagrids. The issue is that the DataGridComboBox doesn't have the DataContext of the DataGrid. I modified the binding for the ComboBox to look like this:

<DataGridComboBoxColumn DisplayMemberPath="Name">
     <DataGridComboBoxColumn.ElementStyle>
             <Style>
                <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=RoadTypes}" />
             </Style>
      </DataGridComboBoxColumn.ElementStyle>
      <DataGridComboBoxColumn.EditingElementStyle>
              <Style>
                  <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=RoadTypes}" />
              </Style>
       </DataGridComboBoxColumn.EditingElementStyle>
  </DataGridComboBoxColumn>

我修改了您在下载链接中提供的代码,并在打开组合框下拉菜单时显示了组合框项目.

I modified the code you provided in your download link and the combobox items were displayed when I opened the combobox dropdown.

查看其中一些链接以获得进一步说明:

Check out some of these links for further clarification:

http:///social.msdn.microsoft.com/Forums/en-US/wpf/thread/b4b13a72-47f9-452f-85c6-6c4b5b606df5/

如何将集合绑定到 WPF:DataGridComboBoxColumn

Excedrin 头痛 #3.5.40128.1:在 WPF DataGrid 中使用组合框

促使我查看所有这些站点的是查看输出"窗口并注意到错误消息 System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement.留言

What led me to look at all of these sites is looking at the Output window and noticing the error message System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. message

这篇关于绑定 DataGridComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆