Home > Net >  How to reset my Static ViewModel from an ApplicationService Class?
How to reset my Static ViewModel from an ApplicationService Class?

Time:09-21

I have multiple Windows in my Project, to have only one Instance of an ViewModel Ive got this Class:

    public sealed class ApplicationService
    {
    private ApplicationService() { }

    public static ApplicationService Instance { get; } = new ApplicationService();

    public ModelView modelView { get; } = new ModelView();
    }

And in my Xaml Windows I declare my DataContext like this:

 DataContext="{Binding modelView, Source={x:Static local:ApplicationService.Instance}}"

ModelView is in this Case the name of my ViewModel

to acces my ViewModel in Code Behind I use this:

var vm = (ModelView)this.DataContext;

The Problem which Ive got now, is that the ViewModel never get Cleared, the Data stays there(I guess it is because of the Static Part).

Anyone an Idea on how to clear/reset the ViewModel?

CodePudding user response:

Add a method to the ModelView class that clears it and call this method, or add a setter to the property of the ApplicationService and assign it to a new instance:

public ModelView modelView { get; set; } = new ModelView();

Clear:

 ApplicationService.Instance.modelView = new ModelView();
  • Related