我是 WPF 新手,目前我正在使用基于 MVVM 的实体框架 6(代码优先)构建一个主从应用程序。
这是用户界面。
主数据网格---类别
详情datagrid --- 产品
我需要的功能:
修改、添加、删除“类别”项或“产品”项
通过“保存”按钮保存到数据库。
用于控制主数据网格中显示项目的类别过滤器。
我将主数据网格绑定到我的视图模型中的 icollectionview“类别”。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFwithEFSampleCodeFirst" mc:Ignorable="d" x:Class="WPFwithEFSampleCodeFirst.MainWindow"
Title="MainWindow" Height="352.134" Width="585.53" Loaded="Window_Loaded">
<Grid Margin="0,0,0,-3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition Width="77*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Button Content="Save" Command="{Binding SaveCmd}" Grid.Column="1" HorizontalAlignment="Left" Margin="425,137,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Grid.ColumnSpan="2"/>
<DataGrid x:Name="MasterGrid" SelectedItem="{Binding SelectedCategory}" ItemsSource="{Binding Categories}" Grid.ColumnSpan="2" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="32,10,0,0" VerticalAlignment="Top" Height="124" Width="330" >
<DataGrid.Columns>
<DataGridTextColumn Width="SizeToHeader" Header="Category Id" Binding="{Binding Path = CategoryId}"/>
<DataGridTextColumn Width="SizeToHeader" Header="Category Name" Binding="{Binding Path = Name}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding SelectedCategoryProducts}" Grid.ColumnSpan="2" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="32,153,0,0" VerticalAlignment="Top" Height="146" Width="330">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding CategoryId}" Header="Category Id" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Product Name" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding ProductId}" Header="Product Id" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
<ComboBox ItemsSource ="{Binding DistinctCategoryName }" SelectedItem="{Binding SelectedCategoryName , Mode = TwoWay }" IsSynchronizedWithCurrentItem="True" Grid.Column="1" HorizontalAlignment="Left" Margin="398,37,0,0" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
<Label Content="Category Name filter" Grid.Column="1" HorizontalAlignment="Left" Margin="398,6,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
</Grid>
视图模型:
class MainWindowViewModel:INotifyPropertyChanged
{
ProductContext context = new ProductContext();
public MainWindowViewModel()
{
IList<Category> categories = GetCategories();
_categoryView = CollectionViewSource.GetDefaultView(categories);
DistinctCategoryName = GetDistinctCategoryName();
_saveCmd = new RelayCommand(Save, CanSave);
}
public IList<Category> GetCategories()
{
return context.Categories.ToList();
}
private ICollectionView _categoryView;
public ICollectionView Categories
{
get { return _categoryView; }
}
private string _selectedCategoryName;
public string SelectedCategoryName
{
get { return _selectedCategoryName; }
set
{
_selectedCategoryName = value;
_categoryView.Filter = new Predicate<object>(GetFilteredView);
_categoryView.Refresh();
OnPropertyChanged("SelectedCategoryName");
}
}
private IList<string> _distinctCategoryName;
public IList<string> DistinctCategoryName
{
get { return _distinctCategoryName; }
set
{
_distinctCategoryName = value;
OnPropertyChanged("DistinctCategoryName");
}
}
private List<string> GetDistinctCategoryName()
{
List<string> distCateName;
List<Category> catelist = context.Categories.ToList();
distCateName = catelist.Select(e => e.Name).Distinct().ToList();
return distCateName;
}
private ICommand _saveCmd;
public ICommand SaveCmd { get { return _saveCmd; } }
public void Save(object obj)
{
foreach (var product in context.Products.Local.ToList())
{
if (product.Category == null)
{
context.Products.Remove(product);
}
}
context.SaveChanges();// What can I do Here to save the category change???
//DistinctCategoryName = GetDistinctCategoryName();
}
public bool CanSave(object obj)
{
return true;
}
public bool GetFilteredView(object sourceObject)
{
Category cate = sourceObject as Category;
if (cate.Name == _selectedCategoryName)
{
return true;
}
return false;
}
private Category _selectedCategory;
public Category SelectedCategory
{
get { return _selectedCategory; }
set
{
_selectedCategory = value;
OnPropertyChanged("SelectedCategory");
OnPropertyChanged("SelectedCategoryProducts");
}
}
public ObservableCollection<Product> SelectedCategoryProducts
{
get
{
if (_selectedCategory == null) return null;
return _selectedCategory.Products;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
//Fire the PropertyChanged event in case somebody subscribed to it
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
问题:
由于过滤器功能,我将 ICollectionView 绑定到 Master Datagrid。添加(添加新行)或删除(按 Delete 键)类别操作无法保存到数据库。(我认为这是因为我正在处理 VIEW 而不是实际源代码。)那么通过 collectionview 添加/删除项目并反映到数据库(源代码)的正确方法是什么?或者我不应该使用 ICollectionView?