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)。
可能是什麼問題?
是的,我看到我的邏輯錯誤。我需要考慮新的設計。感謝您的澄清 – Ofir 2013-04-08 18:02:00