Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

c# - Binding DataGridComboBoxColumn

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>

This is ViewModel and 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.
What's wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" 

to your combobox declartion

--

Update:

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/

How to bind collection to WPF:DataGridComboBoxColumn

Excedrin headache #3.5.40128.1: Using combo boxes with the 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. message


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...