[英]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>
在以下情況下將起作用:
DataGrid
在具有ArtikliStoritveViewModel
實例設置為其DataContext
的Window
中 DDVData
屬性在ArtikliStoritveViewModel
DDV
類具有在其中聲明的名為ArtikliStoritveData
的集合屬性。 如果不是這種情況,那么這將行不通。 您說我在模型視圖中還有其他一些Observable集合,所以我不能在DDV模型中將其更改為DDVDData ...我不知道您對模型視圖的意思是什么,但是如果您將Window.DataContext
設置為您的實例ArtikliStoritveViewModel
中, 這就是它可以訪問。 因此,您需要將集合添加到DDV
類或ArtikliStoritveViewModel
類中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.