У меня есть проект WPF, который читает записи базы данных. Я построил модель с EF 6 из базы данных.
Теперь я немного запутался, как применить шаблон MVVM.
Это класс модели , созданный из EF 6:
public partial class Folder
{
public string FolderPath { get; set; }
public string UserGroup { get; set; }
public string Permission { get; set; }
public string AccessControllType { get; set; }
public string IsInherited { get; set; }
public int FolderId { get; set; }
}
Это MainWindow.xaml.cs :
public partial class MainWindow : Window
{
private ADAccessRightsEntities _context = new ADAccessRightsEntities();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource folderViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("folderViewSource")));
folderViewSource.Source = _context.Folders.ToList();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
this._context.Dispose();
}
}
Это мой код xaml:
<Window x:Class="PermissoinViewer.MainWindow"
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:PermissoinViewer"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="folderViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Folder}, CreateList=True}"/>
<CollectionViewSource x:Key="userGroupViewSource" d:DesignSource="{d:DesignInstance {x:Type local:UserGroup}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource folderViewSource}">
<DataGrid x:Name="folderDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10,10,313,209" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="accessControllTypeColumn" Binding="{Binding AccessControllType}" Header="Access Controll Type" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="folderIdColumn" Binding="{Binding FolderId}" Header="Folder Id" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="folderPathColumn" Binding="{Binding FolderPath}" Header="Folder Path" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="isInheritedColumn" Binding="{Binding IsInherited}" Header="Is Inherited" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="permissionColumn" Binding="{Binding Permission}" Header="Permission" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="userGroupColumn" Binding="{Binding UserGroup}" Header="User Group" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Как мне теперь добавить класс VM?
Edit1: Я создал класс FolderVM.
class FolderVM : AViewModel
{
private Folder folder;
public FolderVM(Folder folder)
{
this.folder = folder;
}
public string FolderPath
{
get => folder.FolderPath;
set
{
folder.FolderPath = value;
CallPropertyChanged();
}
}
public string UserGroup
{
get => folder.UserGroup;
set
{
folder.UserGroup = value;
CallPropertyChanged();
}
}
public string Permission
{
get => folder.Permission;
set
{
folder.Permission = value;
CallPropertyChanged();
}
}
public string AccessControllType
{
get => folder.AccessControllType;
set
{
folder.AccessControllType = value;
CallPropertyChanged();
}
}
public string IsInherited
{
get => folder.IsInherited;
set
{
folder.IsInherited = value;
CallPropertyChanged();
}
}
public int FolderId
{
get => folder.FolderId;
set
{
folder.FolderId = value;
CallPropertyChanged();
}
}
}
Модель AView:
abstract class AViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void CallPropertyChanged([CallerMemberName] string property = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
Я хочу отфильтровать их, чтобы в представлении отображалась только c UserGroup. Как мне продолжить?