2013-04-08 109 views
0

我正在开发基于MVVM的WPF应用程序。 我需要创建2个ComboBox列的DataGrid。GridView不向行添加行

我创建的下一个网格:

<DataGrid Grid.Column="1" Grid.Row="4" AutoGenerateColumns="False" Margin="0,8,20,8" CanUserAddRows="True" CanUserDeleteRows="True" ItemsSource="{Binding MapsGrid}"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Main Category" Width="*"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox 
       ItemsSource="{Binding DataContext.MainCategories, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
       DisplayMemberPath="Category" 
           SelectedItem="{Binding DataContext.MainCategorySelectedItem, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

       <DataGridTemplateColumn Header="Sub Category" Width="*"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox 
       ItemsSource="{Binding DataContext.SubCategories, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
       DisplayMemberPath="Category" 
            SelectedItem="{Binding DataContext.SubCategorySelectedItem, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
           /> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 

     </DataGrid> 

网格长相酷似我需要和组合框控件中包含的数据,但我不`吨知道为什么网格不能插入新行到我的收藏。

在我的视图模型我的下一个集合:

 private ObservableCollection<MapsDescGridModel> _mapsGrid; 
      public ObservableCollection<MapsDescGridModel> MapsGrid 
      { 
       get { return _mapsGrid; } 
       set 
       { 
        if (Equals(value, _mapsGrid)) return; 
        _mapsGrid = value; 
        RaisePropertyChanged("MapsGrid"); 
       } 
      } 

我initiliaze它在我的构造函数,我在我的DataGrid中看到空白行,但我不能添加行(我真的想用回车键)

对象“MapsDescGridModel”包含2个实体(实体框架的实体)

public class MapsDescGridModel: NotificationObject 
    { 
     public MapsDescGridModel() 
     { 

     } 

     public MapsDescGridModel(MainCategories mainCat, SubCategories subcat) 
     { 
      MainCategory = mainCat; 
      SubCatergory = subcat; 
     } 

     private MainCategories _mainCategory; 
     public MainCategories MainCategory 
     { 
      get { return _mainCategory; } 
      set 
      { 
       if (Equals(value, _mainCategory)) return; 
       _mainCategory = value; 
       RaisePropertyChanged("MainCategory"); 
      } 
     } 


     private SubCategories _subCatergory; 
     public SubCategories SubCatergory 
     { 
      get { return _subCatergory; } 
      set 
      { 
       if (Equals(value, _subCatergory)) return; 
       _subCatergory = value; 
       RaisePropertyChanged("SubCatergory"); 
      } 
     } 

    } 
} 

我试图通过代码来添加列,但我只能看到一排(所有剩下的都是警察这一行的y)。 All my rows are copy of one row blank row at start

可能是什么问题?

回答

0

DataGridColumns不会被添加到可视化树中。这就是为什么使用vt的绑定(例如ElementName,FindAncestor绑定)不能在那里工作。 这个问题看起来类似于this之一。

btw .:如果有多行但只有一个DataContext,将每行的SelectedItem绑定到主DataContext是否有意义?您应该将每行的SelectedItem绑定到MapsDescGridModel类中的属性。

+0

是的,我看到我的逻辑错误。我需要考虑新的设计。感谢您的澄清 – Ofir 2013-04-08 18:02:00