[英]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: 在以下情况下将起作用:
DataGrid
is in the Window
that has the ArtikliStoritveViewModel
instance set as its DataContext
DataGrid
在具有ArtikliStoritveViewModel
实例设置为其DataContext
的Window
中 DDVData
property is in the ArtikliStoritveViewModel
DDVData
属性在ArtikliStoritveViewModel
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.