How to reuse a data source object for multiple WPF EF MVVM views

Hi, I am developing an application using WPF4, EF and MVVM. I want to be able to create reusable UserControls that I can use in multiple application windows and make them retrieve data from a single source.

Suppose I have a GraphView component and a TableView component that can appear on the same page or in different places in the application, and I want them to reflect the same set of filtered EF objects. As a rule, the general practice of MVVM requires that each point of view has its own viewing model. But should I use a shared view model and bind it to it, so if you change the data or the filter, both will be updated at the same time? If not, how do I do this?

Thanks for any advice!

+3
source share
4 answers

, ViewModels, /UserControls, ViewModel. , , MainWindow, :

public class MainWindowViewModel
{
    public MainWindowViewModel(IRepository repository)
    {
        SharedUserControlData sharedData = new SharedUserControlData()
        {
           MyCollection = new ObservableCollection<MyEntity>(
               repository.GetMyEntities()),
           // instantiate other shared data properties
        }

        UserControl1ViewModel = new UserControl1ViewModel(sharedData);
        UserControl2ViewModel = new UserControl2ViewModel(sharedData);
    }

    public UserControl1ViewModel UserControl1ViewModel { get; private set; }
    public UserControl2ViewModel UserControl2ViewModel { get; private set; }

    // more stuff...
}

SharedUserControlData, , :

public class SharedUserControlData : INotifyPropertyChanged
{
    public ObservableCollection<MyEntity> MyCollection { get; set; }
    // other properties
    // INotifyPropertyChanged implementation
}

ViewModels UserControls :

public class UserControl1ViewModel
{
    public UserControl1ViewModel(SharedUserControlData data)
    {
        SharedUserControlData = data;
    }

    public SharedUserControlData SharedUserControlData { get; private set; }

    // more stuff
}

// and the same for UserControl2ViewModel

UserControl Views ViewModels DataTemplate:

<DataTemplate DataType="{x:Type vm:UserControl1ViewModel}" >
    <v:UserControl1View />
</DataTemplate>

// and the same for UserControl2ViewModel

UserControls SharedUserControlData.MyCollection UserControlViewModels. DataContext MainWindow - MainWindowViewModel:

IRepository repository = new MyRepository(); // or use Dependency Injection
MainWindow window = new MainWindow();
MainWindowViewModel viewModel = new MainWindowViewModel(repository);
window.DataContext = viewModel;

XAML MainWindow UserControls ViewModels MainWindow DataContext ( MainWindowViewModel):

<StackPanel>
    <v:UserControl1View DataContext="{Binding UserControl1ViewModel}" />
    <v:UserControl2View DataContext="{Binding UserControl2ViewModel}" />
</StackPanel>

, UserControls ViewModels, SharedData, ViewModel , UserControl ViewModels. EF. ( , - .)

+2

EF, EF, . , , viewmodels. , .

, Unity, MEF - , . EF EF . EF , , - .

+2

ViewModel per View. (, ), DI ( , ), App.xaml. cs, . , DI, , .

+1

, BookLibrary WPF Application Framework (WAF). (BookListView [Master], BookView [Detail]) , Entity Framework.

0
source

All Articles