Save and Load WPF UI data with JSON Serialization/Deserialization
<p>I'm making a UI which I want to save/load all dependecyproperties data.</p>

<p>For this I have a class which is my datacontext :</p>

<pre><code>public class CMiX_Data : DependencyObject
{
    public static readonly DependencyProperty TestProperty =
    DependencyProperty.Register("Test", typeof(ObservableCollection&lt;bool&gt;), typeof(CMiX_UI), new PropertyMetadata(new ObservableCollection&lt;bool&gt;(new[] { false, false, false, false, false, false })));
    [Bindable(true)]
    public ObservableCollection&lt;bool&gt; Test
    {
        get { return (ObservableCollection&lt;bool&gt;)this.GetValue(TestProperty); }
        set { this.SetValue(TestProperty, value); }
    }

    public static readonly DependencyProperty Test2Property =
    DependencyProperty.Register("Test2", typeof(ObservableCollection&lt;bool&gt;), typeof(CMiX_UI), new PropertyMetadata(new ObservableCollection&lt;bool&gt;(new[] { false, false, false, false, false, false })));
    [Bindable(true)]
    public ObservableCollection&lt;bool&gt; Test2
    {
        get { return (ObservableCollection&lt;bool&gt;)this.GetValue(Test2Property); }
        set { this.SetValue(Test2Property, value); }
    }
}
}
</code></pre>

<p>Now here is the class who stores JSON method for serialization/deserialization :</p>

<pre><code>public static class JsonSerialization
{
    public static void WriteToJsonFile&lt;T&gt;(string filePath, T objectToWrite, bool append = false) where T : new()
    {
        TextWriter writer = null;
        try
        {
            var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite);
            writer = new StreamWriter(filePath, append);
            writer.Write(contentsToWriteToFile);
        }
        finally
        {
            if (writer != null)
                writer.Close();
        }
    }

    public static T ReadFromJsonFile&lt;T&gt;(string filePath) where T : new()
    {
        TextReader reader = null;
        try
        {
            reader = new StreamReader(filePath);
            var fileContents = reader.ReadToEnd();
            return JsonConvert.DeserializeObject&lt;T&gt;(fileContents);
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
    }
}
}
</code></pre>

<p>Finally this is the usercontrol class where the events for load and save takes place :</p>

<pre><code>    public partial class CMiX_UI : UserControl, INotifyPropertyChanged, ISupportsUndo
{
    private event EventHandler OnSelect;
    public event PropertyChangedEventHandler PropertyChanged;
    CMiX_UserControl.Properties.Settings Pouet = new CMiX_UserControl.Properties.Settings();

    CMiX_Data data = new CMiX_Data();

    public CMiX_UI()
    {
        InitializeComponent();
        this.DataContext = data;
    }

    private void MenuSave_Click(object sender, RoutedEventArgs e)
    {
        JsonSerialization.WriteToJsonFile&lt;CMiX_Data&gt;(@"D:\pouet.txt", data, true);
    }

    private void MenuLoad_Click(object sender, RoutedEventArgs e)
    {
        data = JsonSerialization.ReadFromJsonFile&lt;CMiX_Data&gt;(@"D:\pouet.txt");
    }
</code></pre>

<p>Both event are fired by two different buttons on the UI. The file containing the properties information is correctly saved .</p>

<p>Problem is : the Load is not doing anything, nothing changed on the UI when I push the button and load the properties data.</p>

<p>I found that this has something to do with DependencyProperty, as for normal properties it is deserialized correctly.</p>