GlobalSettings ViewModel

I have some global options that should appear from the ViewModel and should be available for all DataTemplates in all UserControls. It includes things like GlobalButtonMargin, GlobalStrokeWidth or GlobalWorkspaceBackgroundColor. These things are in the viewmodel because the user can edit these settings at runtime.

How are you going to implement this in a good MVVM style?

I was thinking about having a Singleton GlobalSettingsViewModel. Is this the preferred approach? If so, how can I use a singleton instance from XAML?

Another way would be to pass GlobalSettings to all the ViewModel instances that exist in my application, so I can access from the models for which I create DataTemplates. But it is unclean.

The third approach is to rotate the ViewModel alltogether approach to determine what is the XAML resource and dynamically set the resources at runtime using FindResource.

Could you sketch, how would you develop an application to support this scenario?

+3
source share
2 answers

I would create a type to represent your ViewModel as a class, and then define its instance as an ApplicationLevel resource. This ensures a single instance for the entire application, and now you can refer to these settings using StaticResource. For example:

<Application xmlns:myNS="clr-namespace:MyNamespace;assembly=MyAssembly" ...>
    <Application.Resources>
        <myNS:MySettings x:Key="Settings" />
    </Application.Resources>
</Application>

And then in windows / controls / templates / etc. you can access an instance of MySettings using:

{Binding Source={StaticResource Settings}, Path=MyProperty}
+3
source

, , x: Static XAML. , -.

, /. IoC, Unity . http://prism.codeplex.com

IoC, , , , . ( ).

var vm = container.Resolve<SomeViewModel>();

public class SomeViewModel
{
  public SomeViewModel(IUnityContainer container)
  {
      ISomeSettings settings = container.Resolve<ISomeSettings>();    
  }
}

EDIT: , :

:

class GlobalSettings : ViewModel
{
    private Thickness m_globalGirth;

    private static GlobalSettings m_instance = new GlobalSettings();

    public GlobalSettings()
    {
        GlobalGirth = new Thickness(2, 2, 2, 2);
    }
    public Thickness GlobalGirth
    {
        get { return m_globalGirth; }
        set
        {
            m_globalGirth = value;
            InvokePropertyChanged("GlobalGirth");
        }
    }

    public static GlobalSettings Instance
    {
        get { return m_instance; }
        set { m_instance = value; }
    }
}

:

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication3="clr-namespace:WpfApplication3"
    Title="Window1" Height="300" Width="300" MouseDoubleClick="Window_MouseDoubleClick">
    <Window.Resources>
        <WpfApplication3:GlobalSettings x:Key="settings" />
    </Window.Resources>
    <Grid>
        <Border BorderThickness="{Binding Source={StaticResource settings}, Path=Instance.GlobalGirth}"
                BorderBrush="Black"
                Width="100"
                Height="100" />
    </Grid>
</Window>
+4

Source: https://habr.com/ru/post/1719261/


All Articles