简体   繁体   中英

Bind ObservableCollection to ComboBox in DataGrid is not working

I have ready many articles which covers binding Observable Collection to ComboBox but I stil can't figure it out why my collection isn't bindng to ComboBox placed in DataGrid.

My Model

class DDV 
{

    public DDV(int ID, string Naziv)
    {
        _ID = ID;
        _Naziv = Naziv;
    }

    private int _ID;
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }

    private string _Naziv;
    public string Naziv
    {
        get { return _Naziv; }
        set { _Naziv = value; }
    }

My ViewModel:

class ArtikliStoritveViewModel : INotifyPropertyChanged
{

    public ArtikliStoritveViewModel()
    {
        DDVData.Add(new DDV(1, "Ceka"));
        DDVData.Add(new DDV(2, "Zeka"));
    }

    private ObservableCollection<DDV> _DDVData = new ObservableCollection<DDV>();
    public ObservableCollection<DDV> DDVData
    {
        get
        {
            return this._DDVData;
        }

        set
        {
            _DDVData = value;
        }
    }

DataContext:

<Window.DataContext>
    <local:ArtikliStoritveViewModel/>
</Window.DataContext>

Binding:

            <DataGridTemplateColumn Header="Test">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox
                            x:Name="cmbDDV"
                            ItemsSource="{Binding DDVData}"
                            DisplayMemberPath="Naziv"
                        />
                        </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

I have observable collection in View model:

    private ObservableCollection<DDV> _DDVData = new ObservableCollection<DDV>();
    public ObservableCollection<DDV> DDVData
    {
        get
        {
            return this._DDVData;
        }

        set
        {
            _DDVData = value;
        }
    }

My DataGrid Bind to tihs View Model:

<DataGrid ItemsSource="{Binding Path=ArtikliStoritveData}

In constructor in View model I bind to collection:

    DDV _ddv;
    public ArtikliStoritveViewModel()
    {
        _ddv = new DDV { ID = 1, Naziv = "Ceka" };
        DDVData.Add(_ddv);
    }

So everything must stay in this View Model.

What else I have to do to make this work. Currently nothing is binding.

Regards, Igor

Your mistake is in your data model. What is available to your DataTemplate is an instance of your DDV class and that has no collection property named DDVData to data bind to. Instead, you need to add a collection to your DDV class that you can data bind to the ComboBox in each row of the DataGrid and then data bind the collection property named DDVData from the view model to the DataGrid.ItemsSource property:

<DataGrid ItemsSource="{Binding CollectionPropertyInViewModel}">
    ...
        <DataGridTemplateColumn Header="Test">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="cmbDDV"
                        ItemsSource="{Binding CollectionPropertyInModelClass}"
                        DisplayMemberPath="Naziv" />
                    </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    ...
</DataGrid>

Incidentally, if you had looked in your Output Window in Visual Studio, you should have seen some errors saying something like there is no property named DDVData found in object DDV or something like that... helpful clues.


UPDATE >>>

I updated the code above to make it clearer. With your information that you just provided, you'd need to do this:

<DataGrid ItemsSource="{Binding DDVData}">
    ...
        <DataGridTemplateColumn Header="Test">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="cmbDDV"
                        ItemsSource="{Binding ArtikliStoritveData}"
                        DisplayMemberPath="Naziv" />
                    </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    ...
</DataGrid>

This will work when:

  1. The DataGrid is in the Window that has the ArtikliStoritveViewModel instance set as its DataContext
  2. The DDVData property is in the ArtikliStoritveViewModel
  3. The DDV class has a collection property declared in it named ArtikliStoritveData

If that is not the case, then this won't work. You said I have some other Observable Collection in Model View, so I can not change it to DDVDData in DDV model ... I have no idea what you mean by Model View , but if you set the Window.DataContext to an instance of you ArtikliStoritveViewModel , the that is what it has access to. So, you'll either need to add the collection into the DDV class, or the ArtikliStoritveViewModel class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM