Paul Karkoska
Paul Karkoska

Reputation: 463

How can I create Application-scope variable that can be set/get anywhere in the application? (WPF/MVVM)

I am writing a WPF application and trying to learn effective MVVM practices. I have MenuItem buttons in my interface (inside MainWindow.xaml) and would like to bind their Command property to an ICommand I have created.

When the application launches, I want a bool (that can be set/get from anywhere in the application) to be set to originally be set to false. When the "File > New" MenuItem is activated, I want the command to fire off and check against the Application-scope variable in its "CanExecute" method, which is then supposed to enable or disable the MenuItem.

My issue is that I cannot figure out where exactly is the best place to store/instantiate/etc this variable (or property?).

I have tried a multitude of different combinations, including implementing a singleton-pattern design with my "Workspace" class (which holds the boolean variable), and also I am now tinkering with static classes and variables.

My code is shown here:

XAML:

<MenuItem Header="_File">
            <MenuItem Header="_New Project" Command="{Binding NewProjectCommand, Source={StaticResource project}}"/>
            <MenuItem Header="_Open Project"/>
</MenuItem>

App.xaml.cs:

public WorkspaceViewModel WorkspaceInfo { get; set; }

public partial class App : Application
{
    public App()
    {
        //Store variable here? How? And what is the best way to expose it to my Commands?
    }

}

WorkspaceViewModel.cs:

class WorkspaceViewModel : Workspace
{

}

NewProjectCommand.cs:

class NewProjectCommand : ICommand
{

    public NewProjectCommand(ProjectViewModel projectViewModel)
    {
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        //Do an evaluation here using the new bool in App.xaml.cs
        //return true/false, based on the evaluation

    }

    public void Execute(object parameter)
    {
        //Perform steps here to switch the bool in App.xaml.cs, and perform other command functionality
    }
}

Workspace.cs:

class Workspace : INotifyPropertyChanged
{
    private bool _isProjectOpen;

    public bool IsProjectOpen
    {
        get { return _isProjectOpen; }
        set
        {
            _isProjectOpen = value;
            OnPropertyChanged();
        }
    }


    #region INotifyPropertyChanged members

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

I am basically wanting to store a true/false value in the highest level of my application, that way I can get the variable in the "CanExecute" and set the variable in the "Execute" methods for all my ICommands, so all of my MenuItems will become enabled and disabled at the appropriate time.

I realize that this is a rather long-winded question, but any help is very much appreciated and if I need to post any additional information, I would be happy to do so!

Thank you all in advance!

Upvotes: 1

Views: 2360

Answers (3)

Clemens
Clemens

Reputation: 128062

An "application-scope variable" can be declared as property in the App class:

public partial class App : Application
{
    public bool YourProperty { get; set; } // is false by default
}

Now the property "can be set/get from anywhere in the application" by

((App)Application.Current).YourProperty = true;

and

var propertyValue = ((App)Application.Current).YourProperty;

Upvotes: 4

Vigneshwaran Markandan
Vigneshwaran Markandan

Reputation: 945

Store the value in the Properties dictionary of the static Application.Current instance:

Application.Current.Properties["NameOfProperty"] = false;
var myProperty = (bool)Application.Current.Properties["NameOfProperty"];

Upvotes: 3

Saurabh
Saurabh

Reputation: 1631

Two Ways
1.] Either save them as Project settings
Right click on project than click Properties and than go to settings
Create your settings here .
enter image description here you can access or manipulate them programatically in your code

Properties.Settings.Default.SavedInputString

2.] Store them in custom xml file
Xml files are easy to create , read and update and its a fast way to load setting at run time

Upvotes: 1

Related Questions