Search code examples
c#wpfmvvmstatic-class

How to make static class which updated itself? (Similar to Mouse)


Mouse, as you know it, is a static class. It has a static property called Mouse.Position. That property, is updated dynamically (binded to mouse, maybe??). (I've read the Mouse documentation in msdn)

I need to build a same class with this scenario.

I am making an application which able to have multiple pages, and each of the pages shared, for example, Margin value.

There're 2 options (I think) to do it :

  1. Make a field in "page" class, and pass the value via constructor (easiest)
  2. Static class, similar to Mouse.Position usage, but, in this case, for example, PageInformation.Margin (PageInformation is a class, not a property nor a field)

I prefer the second option since it's easier to debug (i guess), and make the Page class cleaner, codeless, and easier during update if the Margin somehow changed by the user..

Anyone know how to do it? (mvvm way preferred)

Thanks.

UPDATE : P.S. I already understand the basic mvvm practice (INPC and such)

This is the general code so far in my application :

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    ...
}

public class SheetPage : ViewModel
{
    ...
}

Solution

  • Your class will be a Singleton and therefor next to impossible to unit test. That does not seem to be a very practical solution.

    In WPF you can bind all margins of all pages to the same ViewModel property. This way, when your ViewModel property changes (assuming you properly implemented INotifyPropertychanged or used DependencyProperty) all your margins will change as well.