We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to serialize the dynamically added children in Docking Manager?

Platform: WPF |
Control: DockingManager

In Docking Manager, the children that are added during run time were not serialized automatically. It can be manually serialized and de-serialized as follows.

Serializing the children

In Window Closing event, Call SaveDockState() method to save the state of static children. Then create a list of string, add the header of all dynamic children and then write it to the xml file.

Deserializing the children

In DockingManager Loaded event, extract the list of headers from xml file. Create child element, set its header and add it to DockingManager. Then call LoadeDockState() method to load all the children.

Here is an example of serializing the children manually using XML Serializer:

MainWindow.xaml

<syncfusion:DockingManager x:Name="docking" UseDocumentContainer="True">
            <Border syncfusion:DockingManager.Header="Window 1" syncfusion:DockingManager.State="Document"/>
</syncfusion:DockingManager>

MainWindow.xaml.cs

public partial class MainWindow : Window
    {
        private int count = 1;
        string filepath = System.IO.Path.GetTempPath() + "Dockstate.xml";
        private static readonly Dictionary<Type, XmlSerializer> _xmlSerializerCache = new Dictionary<Type, XmlSerializer>();
        private readonly string m_StoreFileName = AppDomain.CurrentDomain.FriendlyName + ".dat";
        public MainWindow()
        {
            InitializeComponent();
            docking.Loaded += dockingManager_Loaded;
            Closing += MainWindow_Closing;
            (docking.DocContainer as DocumentContainer).Loaded += DocContainer _Loaded;
        }
        void DocContainer _Loaded(object sender, RoutedEventArgs e)
        {
            DocumentTabControl tabControl = VisualUtils.FindDescendant(docking.DocContainer as DocumentContainer, typeof (DocumentTabControl)) as DocumentTabControl;
            if (tabControl != null)
            {
                tabControl.IsNewButtonEnabled = true;
                tabControl.NewButtonClick += tabControl_NewButtonClick;
            }
        }
        void tabControl_NewButtonClick(object sender, EventArgs e)
        {
            ContentControl control = new ContentControl();
            DockingManager.SetHeader(control, "Window " + ++count);
            DockingManager.SetState(control, DockState.Document);
            docking.Children.Add(control);
        }
        void dockingManager_Loaded(object sender, RoutedEventArgs e)
        {
            Stream stream;
            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
                if (isoStorage!=null && !string.IsNullOrEmpty(m_StoreFileName)
                    && 0 < isoStorage.GetFileNames(m_StoreFileName).Length)
                {
                    stream = new IsolatedStorageFileStream(m_StoreFileName, FileMode.OpenOrCreate, isoStorage);
                    try
                    {
                        XmlTextReader xmlTextReader = new XmlTextReader(stream);
                        LoadState(xmlTextReader);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
            }
            catch (Exception)
            { }
            if (File.Exists(filepath))
                docking.LoadDockState(filepath);
        }
        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            docking.SaveDockState(filepath);
            IsolatedStorageFile isoStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            if (null != isoStorage && !string.IsNullOrEmpty(m_StoreFileName))
            {
                Stream stream = new IsolatedStorageFileStream(m_StoreFileName, FileMode.Create, isoStorage);
                try
                {
                    XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
                    WriteDateToWriter(writer);
                    writer.Flush();
                }
                finally
                {
                    stream.Close();
                }
            }
        }
        private bool LoadState(XmlReader reader)
        {
            XmlSerializer serializer = CreateDefaultXmlSerializer(typeof(List<string>));
 
            if (serializer.CanDeserialize(reader))
            {
                List<string> dockingParamsList = (List<string>)serializer.Deserialize(reader);
                foreach (var header in dockingParamsList)
                {
                    ContentControl control = new ContentControl();
                    DockingManager.SetHeader(control, header);
                    docking.Children.Add(control);
                    count = Int32.Parse(header[header.Length - 1].ToString());
                }
            }
            return true;
        }
        private void WriteDateToWriter(XmlWriter writer)
        {
            List<string> dockingParamsList = new List<string>();
            foreach (var child in docking.Children)
            {
                if (child is ContentControl)
                {
                    if (!dockingParamsList.Contains(DockingManager.GetHeader(child as DependencyObject).ToString()))
                        dockingParamsList.Add(DockingManager.GetHeader(child as DependencyObject).ToString());
                }
            }
            XmlSerializer serializer = CreateDefaultXmlSerializer(typeof(List<string>));
            serializer.Serialize(writer, dockingParamsList);
        }
        public static XmlSerializer CreateDefaultXmlSerializer(Type type)
        {
            XmlSerializer serializer;
            if (_xmlSerializerCache.TryGetValue(type, out serializer))
            {
                return serializer;
            }
            else
            {
                var importer = new XmlReflectionImporter();
                var mapping = importer.ImportTypeMapping(type, null, null);
                serializer = new XmlSerializer(mapping);
                return _xmlSerializerCache[type] = serializer;
            }
        }
    }

 

Samples

C#: DockingManager_Serialization_C#

VB: DockingManager_Serialization_VB

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile