简体   繁体   English

绑定DataGridComboBoxColumn

[英]Binding DataGridComboBoxColumn

I am trying to bind ObservableCollection of T to DataGridComboBoxColumn of DataGrid. 我正在尝试将T的ObservableCollection绑定到DataGrid的DataGridComboBoxColumn。
DataGrid definition is : DataGrid的定义是:

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

This is ViewModel and Model 这是ViewModel和Model

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 displays text column as well but it doesn't display ComboBox values. DataGrid也显示文本列,但不显示ComboBox值。
What's wrong? 怎么了?

Since RoadTypes isn't a simple list of strings you need to tell your combobox what property it needs to display in the ComboBox. 由于RoadTypes不是一个简单的字符串列表,您需要告诉您的组合框需要在ComboBox中显示哪些属性。 Try adding 尝试添加

DisplayMemberPath="Name" 

to your combobox declartion 到您的组合框声明

-- -

Update: 更新:

Okay, this is a known "feature" with WPF datagrids. 好的,这是WPF数据网格的已知“功能”。 The issue is that the DataGridComboBox doesn't have the DataContext of the DataGrid. 问题是DataGridComboBox没有DataGrid的DataContext。 I modified the binding for the ComboBox to look like this: 我修改了ComboBox的绑定,如下所示:

<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/ http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b4b13a72-47f9-452f-85c6-6c4b5b606df5/

How to bind collection to WPF:DataGridComboBoxColumn 如何将集合绑定到WPF:DataGridComboBoxColumn

Excedrin headache #3.5.40128.1: Using combo boxes with the WPF DataGrid Excedrin头痛#3.5.40128.1:使用带有WPF DataGrid的组合框

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. 让我看看所有这些站点的原因是查看“输出”窗口并注意到错误消息System.Windows.Data错误:2:无法找到目标元素的管理FrameworkElement或FrameworkContentElement。 message 信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM