Binding WPF Datagrid to List Tasks

I have a list of user objects: let's say Fruits with two string properties Name and Color. They are on the list.

private readonly List<Fruit> fruitList = new List<Fruit>();

Then I load the fruit objects into the list.

I am trying to associate this list with WPF Datagrid:

FROM#:

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>

The reason they are in combobox is because I want the user to be able to change relationships. This is not a real example, but you understood this idea. Say, for the sake of example, the fruits were not ripe, so they change the color of the banana to green :)

I will not be able to get these elements in a datagrid ... and down the track, I want to click an element in a datagridcell to go to combobox and show all possible types of fruit names and colors (this way they can change relationships)

Here is the error I get:

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')

- ? , xaml, - , .

, ObservableCollection, , ?

, knwo,

: :

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>

#:

DataContext = fruitList;

, DataContext, . ItemSource, , , .

, , . autogeneratecolumns false, xaml, , , !

+5
2

-, :

dgFruit.ItemsSource = "{Binding}"

, Binding . , ( , , , !). ItemsSource XAML , , . :

<toolkit:DataGrid Name="dgFruit"       
                  ItemsSource="{Binding}">
    ...
</toolkit:DataGrid>

:

...
    InitializeComponents();
    this.DataContext = fruitList;
...

- List <Fruit> , ItemSource - . , fruitList .

:

        <toolkit:DataGridTextColumn Header="Name"
                                    Binding="{Binding Name}"
                                    Width="5*" />
        <toolkit:DataGridComboBoxColumn Header="Color"
                                        ItemsSource="{Binding Source={StaticResource AllColors}}"
                                        SelectedValueBinding="{Binding Path=Color}"
                                        TextBinding="{Binding Path=Color}"
                                        Width="5*" />

AllColors ( ):

<x:Array x:Key="AllColors"
         Type="sys:String">
    <sys:String>Orange</sys:String>
    <sys:String>Yellow</sys:String>
</x:Array>

.

+4

WPF . , fruitList .

    // this is the field. you can't bind to this
    private readonly List<Fruit> fruitList = new List<Fruit>();

    // this is the property. you can bind to this
    public List<Fruit> FruitList
    {
        get { return fruitList; }
    }

dgFruit.DataContext = this; dgFruit.ItemsSource = "{Binding}";

ComboBox, DataGridComboBoxColumn , . ,

public class Fruit
{
    public string Name { get; set; }
    public string Color { get; set; } // bind the combobox selectedvalue to this

    public List<string> AvailableColors { get; set; } // bind the combobox ItemsSource to this
}

List, . ObservableCollection , INotifyCollectionChanged INotifyPropertyChanged

+4

All Articles