简体   繁体   English

将ObservableCollection绑定到DataGrid中的ComboBox不起作用

[英]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. 我已经准备好许多文章,涵盖将Observable Collection绑定到ComboBox的问题,但我仍无法弄清楚为什么我的收藏集没有绑定到放置在DataGrid中的ComboBox。

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: 我的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: 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: 我在View模型中有可观察的集合:

    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绑定到此视图模型:

<DataGrid ItemsSource="{Binding Path=ArtikliStoritveData}

In constructor in View model I bind to collection: 在View模型的构造函数中,我绑定到集合:

    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. DataTemplate可用的是DDV类的实例,并且没有名为DDVData集合属性可绑定到数据。 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: 相反,您需要向DDV类添加一个集合,可以将其数据绑定到DataGrid每行中的ComboBox ,然后将视图模型中名为DDVData的集合属性绑定到DataGrid.ItemsSource属性:

<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. 顺便说一句,如果您在Visual Studio的“输出”窗口中查看过,您应该会看到一些错误,例如在对象DDV找不到名为DDVData属性或类似的...有用的提示。


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 DataGrid在具有ArtikliStoritveViewModel实例设置为其DataContextWindow
  2. The DDVData property is in the ArtikliStoritveViewModel DDVData属性在ArtikliStoritveViewModel
  3. The DDV class has a collection property declared in it named ArtikliStoritveData DDV类具有在其中声明的名为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. 您说我在模型视图中还有其他一些Observable集合,所以我不能在DDV模型中将其更改为DDVDData ...我不知道您对模型视图的意思是什么,但是如果您将Window.DataContext设置为您的实例ArtikliStoritveViewModel中, 就是它可以访问。 So, you'll either need to add the collection into the DDV class, or the ArtikliStoritveViewModel class. 因此,您需要将集合添加到DDV类或ArtikliStoritveViewModel类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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