1

In my datagrid, one of columns is DataGridComboBoxColumn, where I'm trying to display different drop down menu in every row. Easy task when creating combo box in XAML instead of doing it programatically. My problem is that I have no idea how to bind it properly. This is what I tried:

private DataGridComboBoxColumn CreateComboValueColumn(List<Elements> elements)
{
    DataGridComboBoxColumn column = new DataGridComboBoxColumn();
    column.ItemsSource = elements;
    column.DisplayMemberPath = "Text";
    column.SelectedValuePath = "ID";
    column.SelectedValueBinding = new Binding("Value");
    return column;
}

public class Elements
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Comment { get; set; }
    public List<ComboItem> ComboItems { get; set; }
}

public class ComboItem
{
    public string ID { get; set; }
    public string Text { get; set; }
}
2
  • why do you avoid xaml? Commented Mar 21, 2018 at 10:36
  • because I have dynamic amount of DataGrids Commented Mar 21, 2018 at 11:43

3 Answers 3

1

You have to think from above and read what you are doing.

    column.ItemsSource = elements;

That sets your column itemssource to a list of elements.

column.DisplayMemberPath = "Text";

It's not a member of Element so it won't show anything. You should set your column.ItemsSource to:

column.ItemsSource = elements[i].ComboItems

Being "i" the element you want to show.

Then if you want to show the text you should:

    column.DisplayMemberPath = "Text";

If you want the Id then just:

    column.DisplayMemberPath = "ID";

I wrote this without any editor and I think this is close to the answer you want, if I'm wrong comment this and I'll try to answer in a more accurate way.

Sign up to request clarification or add additional context in comments.

2 Comments

you are right at pointing out that column.ItemsSource = elements[i].ComboItems should make it work. And it would, if I knew how to tell XML that i = column.row
You get your items by XML ?
0

It seems that adding binding from style works better than direct approach. This works:

private DataGridComboBoxColumn CreateComboValueColumn(List<Elements> elements)
{
    DataGridComboBoxColumn column = new DataGridComboBoxColumn();

    Style style = new Style(typeof(ComboBox));
    //set itemsource = {Binding ComboItems}
    style.Setters.Add(new Setter(ComboBox.ItemsSourceProperty, new Binding("ComboItems")));
    column.DisplayMemberPath = "Text";
    column.SelectedValuePath = "ID";
    column.SelectedValueBinding = new Binding("Value");

    column.ElementStyle = style;
    column.EditingElementStyle = style;
    return column;
}

Comments

0

You can use the following as an example:

    ComboBox dgvCmb = new ComboBox();
    dgvCmb.Name = "Name";
    dgvCmb.Items.Add("Razali");
    dgvCmb.Items.Add("Boeing");
    dgvCmb.Items.Add("Londang");
    dgvCmb.Items.Add("MCKK");
    
    DataGridComboBoxColumn   dgc=new DataGridComboBoxColumn();
    dgc.Header = "Test";
    dgc.ItemsSource = dgvCmb.Items;
    dataGrid.Columns.Add(dgc);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.