繁体   English   中英

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

[英]Bind ObservableCollection to ComboBox in DataGrid is not working

我已经准备好许多文章,涵盖将Observable Collection绑定到ComboBox的问题,但我仍无法弄清楚为什么我的收藏集没有绑定到放置在DataGrid中的ComboBox。

我的模特

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; }
    }

我的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>

捆绑:

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

我在View模型中有可观察的集合:

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

        set
        {
            _DDVData = value;
        }
    }

我的DataGrid绑定到此视图模型:

<DataGrid ItemsSource="{Binding Path=ArtikliStoritveData}

在View模型的构造函数中,我绑定到集合:

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

因此,所有内容都必须保留在此视图模型中。

为了使这项工作我还要做些什么。 目前没有任何约束力。

问候,伊戈尔

您的错误在于您的数据模型中。 DataTemplate可用的是DDV类的实例,并且没有名为DDVData集合属性可绑定到数据。 相反,您需要向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>

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


更新>>>

我更新了上面的代码以使其更加清晰。 使用您刚刚提供的信息,您需要执行以下操作:

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

在以下情况下将起作用:

  1. DataGrid在具有ArtikliStoritveViewModel实例设置为其DataContextWindow
  2. DDVData属性在ArtikliStoritveViewModel
  3. DDV类具有在其中声明的名为ArtikliStoritveData的集合属性。

如果不是这种情况,那么这将行不通。 您说我在模型视图中还有其他一些Observable集合,所以我不能在DDV模型中将其更改为DDVDData ...我不知道您对模型视图的意思是什么,但是如果您将Window.DataContext设置为您的实例ArtikliStoritveViewModel中, 就是它可以访问。 因此,您需要将集合添加到DDV类或ArtikliStoritveViewModel类中。

暂无
暂无

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

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