2016-06-08 46 views
0

我有一個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包含ValueCategory,但我希望用戶能夠選擇項目。我試過這個:

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

+0

了''StoredSequences'商店ObservableCollection''StoredSequences'-ES或'Sequence'?我不完全明白。順便說一句,我會這樣做,像'StoredSequences'是視圖模型的'ObservableCollection' – ntohl

+0

@ntohl它存儲序列。 –

+0

你能粘貼Sequence類嗎?我對價值和類別屬性感興趣 – ntohl

回答

0

您可以從錯誤消息中看到綁定失敗,因爲框架無法在Sequence類型的對象上找到值和類別屬性。發生這種情況是因爲DataGrid中的每一行都將底層序列作爲其DataContext。

如果您不想將Values和Categories集合添加到Sequence對象,那麼您需要做的是更改綁定,以便它們綁定到父級DataContext,而不是行DataContext。

試試這個例子中,會被重定向綁定到DataGrid的DataContext的:

 <DataGridTemplateColumn> 
      <DataGridTemplateColumn.CellEditingTemplate> 
       <DataTemplate> 
        <ComboBox x:Name="ComboBox2" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Categories}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellEditingTemplate> 
     </DataGridTemplateColumn> 
+0

謝謝!它適用於'DataContext.Categories'。 –

+0

對不起,我應該已經發現了。很高興它幫助你。 –