Sreginogemoh
Sreginogemoh

Reputation: 1309

Share the same object between two windows in WPF

I have class representing my AppSettings I have Main window and Settings window.

Each window contains instance of object AppSettings

So these are two objects are different. If object AppSettings in Settings window gets changed the changes not reflected in the AppSettings of the Main window.

IS there any way i can share AppSettings object between windows so i have only one instance?

I've tried to create shared base class but got an error

Partial declarations of "class name" must not specify different base classes    

Upvotes: 2

Views: 4043

Answers (5)

Zeus
Zeus

Reputation: 1275

I know this answer is bump to the topic but I found other easier way of doing what is asked so that could help anyone in future. In every WPF application there is app.xaml and app.xaml.cs created. So create a object of setting inside app.xaml.cs and it would look something like this :

namespace WpfApplication
{    
    public partial class App : Application
    {
        // Settings :
        public int setting_1 { get; set; } //some setting variable
        public string setting_2 { get; set; } //some other setting variable
    }
}     

Now to refer to this object from different window you can use : ((App)Application.Current).setting_1

Upvotes: 2

Alexandre
Alexandre

Reputation: 498

You could have a class like "Utils" on your project with a property like:

Public Shared(or Static in C#) AppSettings  As YourObjectType

And then in the xaml of the windows make a binding in mode two way with Utils.AppSetings

Upvotes: 0

Tony Vitabile
Tony Vitabile

Reputation: 8614

Sure there is. You can make the AppSettings property in both windows DependencyProprerties and then bind the property in the Settings to the Main window's property when you create the Settings window. That is, in the SettingsWindow class:

partial class SettingsWindow : Window {

    public static readonly DependencyProperty AppSettingsProperty("AppSettings", typeof(AppSettings), typeof(SettingsWindow), new PropertyMetaData(null));

    public AppSettings AppSettings {
        get { return (AppSettings) GetValue(AppSettingsProperty); }
        set { SetValue(AppSettingsProperty, value); }
    }

}

Then, in the Main window class's code behind:

partial class MainWindow : Window {

    public static readonly DependencyProperty AppSettingsProperty("AppSettings", typeof(AppSettings), typeof(MainWindow), new PropertyMetaData(null));

    public AppSettings AppSettings {
        get { return (AppSettings) GetValue(AppSettingsProperty); }
        set { SetValue(AppSettingsProperty, value); }
    }

    private void ShowSettingsWindowButton_Click(object sender, RoutedEventArgs e ) {
        SettingsWindow settingsWindow = new SettingsWindow();
        Binding appSettingsBinding = new Binding("AppSettings");
        appSettingsBinding.Source = this;
        appSettingsBinding.Path = new PropertyPath( "AppSettings" );
        appSettingsBinding.Mode = BindingMode.TwoWay;
        BindingOperations.SetBinding( this, AppSettingsProperty, appSettingsBinding );
        settingsWindow.ShowDialog();
    }
}

The Binding mechanism will keep the properties in both objects in synch. So if you replace the value of the property in one class with a different instance while the SettingsWindow is open, the SettingsWindows will be notified of the change and update its copy.

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18578

You can create the Static Property in one class and create the wrapper property on that static property in other class.

Also if you are binding this property to your UI, then you dont need two propeties.. you can bind to the static instance.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564821

IS there any way i can share AppSettings object between windows so i have only one instance?

You'd need some way for both windows to get the same reference. If you pass a reference to the same AppSettings object to both Windows, this should just work.

Upvotes: 1

Related Questions