I have a list of custom objects: say Fruits with two string properties Name and Color. These are in a List.
private readonly List<Fruit> fruitList = new List<Fruit>();
Then I load fruit objects into the list.
I am trying to bind this list to WPF Datagrid:
C#:
dgFruit.ItemsSource = "{Binding}";
XAML:
<toolkit:DataGrid Name="dgFruit"
ItemsSource="{Binding Path=fruitList}" >
<toolkit:DataGrid.Columns>
<toolkit:DataGridComboBoxColumn
Header="Name"
SelectedValueBinding="{Binding Path=Name}"
TextBinding="{Binding Path=Name}" Width="5*" />
<toolkit:DataGridComboBoxColumn
Header="Color"
SelectedValueBinding="{Binding Path=Color}"
TextBinding="{Binding Path=Color}" Width="5*" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
Reason they are in a combobox is because I want user to be able to change the relationship. This is not the real example but you get the idea. Say for the sake of the example the fruit wasn't ripe so they change Banana color to green :)
I'm not having any luck getting these items in the datagrid... and down the track, I want a click on the item in the datagridcell to change to combobox and show all possible types of Fruit Names and Colors (so they can change the relationships)
Here is an error i'm getting:
System.Windows.Data Error: 39 : BindingExpression path error: 'Color' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Color; DataItem='Char' (HashCode=6750311); target element is 'TextBlockComboBox' (Name=''); target property is 'Text' (type 'String')
Can anyone help? The reason i am setting my colums up in xaml is so i can set width to star size and have the columns equal width.
I see most examples use an ObservableCollection but if I can bind to list why do I have to use this?
Please let me knwo if my example requires further clarification
Edit: what I have now:
XAML:
<toolkit:DataGrid Name="dgFruit"
ItemsSource="{Binding}"
AutoGenerateColumns="False">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn
Header="Name"
Width="5*"/>
<toolkit:DataGridComboBoxColumn
Header="Color"
Width="5*"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
C#:
DataContext = fruitList;
Actually when I use DataContext I get no items. When I use ItemSource I get blank lines, but the correct number of rows so this seems more along the right lines.
I can get the data to show if I let it Auto generate columns. If I set autogeneratecolumns to false and then specify my columns in xaml like i want to to use star sizing, I get the right number of columns but no text!