views:

248

answers:

1

Is it possible to persist a WPF Control, or any other non-trivial object to a *.settings file in a .NET application? I figured this would be easy since the *.settings file allows me to specify an atrbitrary type for each individual settings, unfortunately, it does not appear to be functioning for me.

What I am trying to do is to persist a System.Windows.Controls.ListBox to my application's *.settings file. The problem is, my listbox instance is not actually being saved. When I go to run my application again, it is just null when I go to load it.

Here is what I am using to load/save my ListBox instance

    private void PersistListBoxToSettings(System.Windows.Controls.ListBox projectsListBox)
    {
        Properties.Settings.Default.ListBoxData = projectsListBox;
        Properties.Settings.Default.Save();
    }

    private void LoadProjectListBox()
    {
        projectsListBox = Properties.Settings.Default.ListBoxData; // Always null after application restart.
    }

I tested my *.settings file to ensure that it was even working by saving/loading a simple string - that functioned as expected.

Is what I am trying to do just a really bad idea? Should I stop trying to be clever and use proper object serialization to disk?

+1  A: 

My belief is that the objects you're saving to a .settings file must be serializable, implementing ISerializable. I doubt that Controls have this implemented. This link seems to back up my thoughts:

Bytes.com Forum

You might be able to make a wrapper for saving it, implementing ISerializable and using [SettingsSerializeAs(SettingsSerializeAs.Binary)] as the link suggestions to get around this.

Will Eddins