【问题标题】:Bind ObservableCollection to ComboBox in DataGrid is not working将 ObservableCollection 绑定到 DataGrid 中的 ComboBox 不起作用
【发布时间】:2014-03-18 21:01:19
【问题描述】:

我已经准备好很多文章,其中包括将 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; }
    }

我的视图模型:

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

数据上下文:

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

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

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

        set
        {
            _DDVData = value;
        }
    }

我的 DataGrid 绑定到视图模型:

<DataGrid ItemsSource="{Binding Path=ArtikliStoritveData}

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

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

所以一切都必须留在这个视图模型中。

我还需要做什么才能完成这项工作。目前没有任何约束力。

问候, 伊戈尔

【问题讨论】:

    标签: c# wpf binding datagrid observablecollection


    【解决方案1】:

    您的错误在于您的数据模型。您的 DataTemplate 可用的是您的 DDV 类的一个实例,并且没有名为 DDVData 的集合属性可用于数据绑定。相反,您需要向您的 DDV 类添加一个集合,您可以将其数据绑定到 DataGrid 的每一行中的 ComboBox,然后将名为 DDVData 的集合属性从视图模型数据绑定到 @ 987654329@财产:

    <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 Collection,所以我不能在 DDV 模型中将其更改为 DDVDData...我不知道你的意思是什么模型视图 ,但是如果您将 Window.DataContext 设置为您的实例 ArtikliStoritveViewModel,则 that 就是它可以访问的内容。因此,您需要将集合添加到 DDV 类或 ArtikliStoritveViewModel 类中。

    【讨论】:

    • 我在模型视图中有一些其他的 Observable 集合,所以我不能在 DDV 模型中将其更改为 DDVDData: public ObservableCollection ArtikliStoritveData { get { return _artikliStoritveData; } 设置 { _artikliStoritveData = 值; OnPropertyChanged("ArtikliStoritveData"); } }
    • 请将您的代码添加到您的 question 的末尾并且 not 在一个 comment 中,这很难看。另外,我真的没听懂你说什么。
    • 我添加了。我在“输出”窗口中没有任何错误或警告。
    • 我明白了。必须将 Observable 集合 DDVDData 添加到数据网格绑定到的 Observable 集合中。
    猜你喜欢
    • 2019-02-12
    • 2013-01-14
    • 2023-03-13
    • 2014-09-02
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多