如何使用MVVM将ICollectionView绑定到datagrid
我是WPF的新手,并遵循此链接使用代码优先方法构建示例。 这个例子起作用。 https://msdn.microsoft.com/en-us/data/jj574514.aspx
现在,我试图改变它遵循MVVM。
这是MainWindow XAML
<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="517.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" Grid.Column="2" HorizontalAlignment="Left" Margin="41,167,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<DataGrid Grid.ColumnSpan="2" ItemsSource="{Binding Categories}" 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="Name" Binding="{Binding Path = Name}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid 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="Name" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding ProductId}" Header="Product Id" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
这是MainWindowViewModel
class MainWindowViewModel
{
private ICollectionView _categoryView;
public ICollectionView Categories
{
get { return _categoryView; }
}
ProductContext context = new ProductContext();
public MainWindowViewModel()
{
IList<Category> categories = GetCategories();
_categoryView = CollectionViewSource.GetDefaultView(categories);
}
public IList<Category> GetCategories()
{
return context.Categories.ToList();
}
}
我不知道如何将第二个细节datagrid绑定到ViewModel。 我想与原始示例具有相同的主站详细信息显示功能。
那么如何将Categories中的产品绑定到第二个数据网格呢? 使用MVVM实现它的正确方法是什么?
更多信息:
public class Category
{
public Category()
{
this.Products = new ObservableCollection<Product>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual ObservableCollection<Product> Products { get; private set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
将主DataGrid的SelectedItem属性绑定到ViewModel中的一个属性
<DataGrid SelectedItem="{Binding SelectedCategory}" Grid.ColumnSpan="2" ItemsSource="{Binding Categories}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="SizeToHeader" Header="Category Id" Binding="{Binding Path = CategoryId}"/>
<DataGridTextColumn Width="SizeToHeader" Header="Name" Binding="{Binding Path = Name}"/>
</DataGrid.Columns>
</DataGrid>
MainWindowViewModel
private Category _selectedCategory;
public Category SelectedCategory
{
get { return _selectedCategory; }
set
{
_selectedCategory = value;
OnPropertyChanged("SelectedCategory");
OnPropertyChanged("SelectedCategoryProducts");
}
}
(这需要您的视图模型实现INotifyPropertyChanged。OnPropertyChanged方法调用PropertyChanged事件处理程序)
添加另一个返回所选类别的产品属性的属性
public ObservableCollection<Product> SelectedCategoryProducts
{
get
{
if (_selectedCategory == null) return null;
return _selectedCategory.Products;
}
}
将细节DataGrid绑定到视图模型中的SelectedCategoryProducts属性
<DataGrid ItemsSource="{Binding SelectedCategoryProducts}" Grid.ColumnSpan="2" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding CategoryId}" Header="Category Id" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding ProductId}" Header="Product Id" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
最简单的方法是将DataGrid的DataContext绑定到主DataGrid的SelectedItem属性
<DataGrid x:Name="MasterGrid" Grid.ColumnSpan="2" ItemsSource="{Binding Categories}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="SizeToHeader" Header="Category Id" Binding="{Binding Path = CategoryId}"/>
<DataGridTextColumn Width="SizeToHeader" Header="Name" Binding="{Binding Path = Name}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid DataContext="{Binding SelectedItem.Products, ElementName=MasterGrid}" Grid.ColumnSpan="2" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding CategoryId}" Header="Category Id" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding ProductId}" Header="Product Id" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
有了ICollectionView
,使用它的功能将会是一件耻辱......
首先,对第一个数据网格(类别)设置IsSynchronizedToCurrentItem="true"
。
然后在你的第二个DataGrid中,将DataSource绑定到ItemsSource="{Binding Categories.CurrentItem.Products}"
其中Categories是您的视图模型ICollectionView
。
IsSynchToCurrentItem=true
的效果是你不需要在viewmodel中保存一个属性来跟踪你的当前项目,因为ICollectionView
可以为你做。
然后,每当用户在数据网格中选择一行时,当前项目将在视图模型中发生变化(并且ICollectionView
上有一个事件需要通知),每当您在视图模型中设置当前项目时,相应的行将会被选中。
除此功能之外,ICollectionView还允许您排序,筛选和分组,而无需触及源集合,最重要的是,它使您能够以编程方式更改当前项目,而无需通过XAML控件显示周围情况 (因此通过使用诸如MoveCurrentTo(object target)
, MovecurrentToFirst()
等等的方法,在对应的visual / XAML itemcontrol中选择的行MovecurrentToFirst()
。
你的商业C#模型很好,所以你的XAML看起来就像这样:
<DataGrid Grid.ColumnSpan="2" IsSynchronizedToCurrentItem="true" ItemsSource="{Binding Categories}" 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="Name" Binding="{Binding Path = Name}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.ColumnSpan="2" ItemsSource="{Binding Categories.CurrentItem.Products}" 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="Name" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding ProductId}" Header="Product Id" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
链接地址: http://www.djcxy.com/p/56153.html