1

I´m currently learning the DataBinding in C# WPF and I´m trying to set the ItemsSource property of a DataGridComboBoxColumn.

I have a Class Material:

public class Material {
    public static List<Material> loadedMaterials;

    static Material() {
        loadedMaterials = new List<Material>();

        loadedMaterials.Add(new Material("TEST1", "", ""));
        loadedMaterials.Add(new Material("TEST2", "", ""));
        loadedMaterials.Add(new Material("TEST3", "", ""));
    }

    public string name { get; set; }
    public string name2 { get; set; }
    public string name3 { get; set; }

    public Material(string n, string n2, string n3) {
        name = n;
        name2 = n2;
        name3 = n3;
    }
}

And a Datagrid:

<DataGrid x:Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Name" SelectedItemBinding="{Binding name}"/>
    </DataGrid.Columns>
</DataGrid>

The SelectedItemBinding is already working, but now I´m trying to set the ItemsSource.

I want all names, of the Materials stored in loadedMaterials.

I tried the following:

//K Refers to The Namespace the Material-Class is in    
ItemsSource="{Binding Source={x:Static K:Material.loadedMaterials}, Path=name}

But instead of getting the List of all the names, the List the dropdown is using, is every Character the first Materials name has. ("T", "E", "S", "T", "1")

Is there a way to do get all the names from all the Materialss or do I have to create an extra List to achive this?

1 Answer 1

2

Remove the , Path=name part from the ItemsSource. The code is binding to the first string, treating it as an array of characters.

To display the name of a material either set the DisplayMemberPath of the column to "name" or create an ItemTemplate for the DataGridComboBoxColumn:

<DataGridComboBoxColumn.CellStyle>
    <Style TargetType="ComboBox">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding name}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridComboBoxColumn.CellStyle>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! DisplayMemberPath was exactly what I was searching for. :)

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.