我有一個DataGrid界到的ObservableCollection(StoredSequences<Sequence>
):如何將DataGrid綁定到更多集合?
<DataGrid ItemsSource="{Binding StoredSequences}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" >
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding NameEnglish}" >
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
的序列模型:
public class Sequence : INotifyPropertyChanged
{
public Sequence() { }
private int _id;
public int ID
{
get
{
return _id;
}
set
{
_id = value;
OnPropertyChanged("ID");
}
}
private string _nameEnglish;
public string NameEnglish
{
get
{
return _nameEnglish;
}
set
{
_nameEnglish = value;
OnPropertyChanged("NameEnglish");
}
}
private int _value;
public int Value
{
get
{
return _value;
}
set
{
_value= value;
OnPropertyChanged("Value");
}
}
private string _category;
public string Category
{
get
{
return _category;
}
set
{
_category = value;
OnPropertyChanged("Category");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我要添加兩個組合框在網格中的最後兩列,用的ItemsSource界到其他ObservableCollections(在ViewModel中生成的Values<int>
和Categories<string>
)。我可以添加文本框,因爲Sequence
包含Value
和Category
,但我希望用戶能夠選擇項目。我試過這個:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBox1" ItemsSource="{Binding Values}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBox2" ItemsSource="{Binding Categories}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
問題是ComboBoxes是空的。這是收到的錯誤之一:
System.Windows.Data Error: 40 : BindingExpression path error: 'Values' property not found on 'object' ''Sequence' (HashCode=31195541)'. BindingExpression:Path=Values; DataItem='Sequence' (HashCode=31195541); target element is 'ComboBox' (Name=ComboBox1'); target property is 'ItemsSource' (type 'IEnumerable')
什麼是綁定到DataGrid到多個集合的正確方法?或者,如何在CellEdit和TextBox上顯示ComboBoxes?
了''StoredSequences'商店ObservableCollection''StoredSequences'-ES或'Sequence'?我不完全明白。順便說一句,我會這樣做,像'StoredSequences'是視圖模型的'ObservableCollection' – ntohl
@ntohl它存儲序列。 –
你能粘貼Sequence類嗎?我對價值和類別屬性感興趣 – ntohl