Reputation: 1307
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 name
s from all the Materials
s or do I have to create an extra List to achive this?
Upvotes: 1
Views: 1681
Reputation: 50672
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>
Upvotes: 2