0

I have an ObservableCollection that gets it's data from a DataTable that is populate from a Postgres Database. I need to bind this ObservableCollection to a ComboBoxColumn in a DataGrid. I have seen quite a lot of examples on how to do this, yet I'm constantly missing something.

Edit: This is the new updated code and it is working except for the INotifyPropertyChanged that I have set only to "name" (yet)

namespace Country_namespace

{

public class CountryList : ObservableCollection<CountryName>
{
    public CountryList():base()
    {

      // Make the DataTables and fill them           



    foreach(DataRow row in country.Rows)
    {
       Add(new CountryName((string)row.ItemArray[1], (int)row.ItemArray[0]));
   }           
    }
}

public class CountryName: INotifyPropertyChanged
{
    private string name;
    private int id_country;
    public event PropertyChangedEventHandler PropertyChanged;

    public CountryName(string country_name, int id)
    {
        this.name = country_name;
        this.id_country = id;
    }

    public string Name
    {
        get { return name; }
        set {
        name = value;
        OnPropertyChanged("CountryName");
        }
    }

    public int idcountry
    {
        get { return id_country; }
        set { id_country = value; }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

}

XAML:

xmlns:c="clr-namespace:Country_namespace"

<Windows.Resources>
<c:CountryList x:Key="CountryListData"/>
</Windows.Resources>

DataGrid Column:

<dg:DataGridTemplateColumn Header="country">
                                <dg:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <ComboBox ItemsSource="{Binding Source={StaticResource CountryListData}}"  DisplayMemberPath="Name"></ComboBox>

                                    </DataTemplate>
                                </dg:DataGridTemplateColumn.CellTemplate>
                            </dg:DataGridTemplateColumn>
boo_boo_bear
  • 189
  • 1
  • 4
  • 14
  • "Datacontext should bet set to the ObservableCollection country_" This might be incorrect. – David May 27 '13 at 05:07
  • No I have not implemented INotifyPropertyChanged. And that is one of the reasons I am asking this question. I am completely puzzeled when it comes to ObservableCollections as I have never used them before. – boo_boo_bear May 27 '13 at 05:49

1 Answers1

0

first of all. you can just bind to public properties.

country_ seems no public property.

second if binding not work you always have to check datacontext first and binding path second. you can use Snoop to do this at runtime

EDIT:

you did not post your itemssource for your grid. so some assumptions here.

<DataGrid ItemsSource="{Binding MySource}">
  ...
    <ComboBox ItemsSource="{Binding MySourcePropertyForCountries}"/>

--> this would work when your MySource object item has a public property MySourcePropertyForCountries.

but if your want to bind your combobox to a list wich is outside the MySource object. then you have to use some kind relativeSourcebinding or elementbinding.

<DataGrid x:Name="grd" ItemsSource="{Binding MySource}">
  ...
    <ComboBox ItemsSource="{Binding ElementName=grd, Path=DataContext.MyCountries}"/>

--> this would work when the datacontext of the datagrid has a property MyCountries

blindmeis
  • 22,175
  • 7
  • 55
  • 74