xaml-sdk/Docking/PaneSourceWithLayout/MainPage.xaml.cs

57 строки
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
namespace PaneSourceWithLayout
{
public partial class MainPage : UserControl
{
private string dockingLayoutXml = @"<?xml version=""1.0"" encoding=""utf-8""?><RadDocking><DocumentHost><RadSplitContainer IsAutoGenerated=""True""><Items><RadPaneGroup IsAutoGenerated=""True"" TabStripPlacement=""Top"" SelectedIndex=""0""><Items><RadPane SerializationTag=""SavedPane2"" IsDockable=""True"" Header=""Saved Pane 2"" /></Items></RadPaneGroup></Items></RadSplitContainer></DocumentHost><SplitContainers><RadSplitContainer Dock=""DockedRight"" Width=""240"" RelativeWidth=""100"" RelativeHeight=""100"" IsAutoGenerated=""True""><Items><RadPaneGroup RelativeWidth=""100"" RelativeHeight=""100"" IsAutoGenerated=""True"" SelectedIndex=""0""><Items><RadPane SerializationTag=""SavedPane1"" IsDockable=""True"" Header=""Saved Pane 1"" /></Items></RadPaneGroup></Items></RadSplitContainer><RadSplitContainer Dock=""DockedLeft"" Width=""240""><Items><RadPaneGroup SelectedIndex=""-1""><Items /></RadPaneGroup></Items></RadSplitContainer><RadSplitContainer Dock=""DockedLeft"" Width=""240""><Items><RadPaneGroup SelectedIndex=""-1""><Items /></RadPaneGroup></Items></RadSplitContainer></SplitContainers></RadDocking>";
public MainPage()
{
InitializeComponent();
}
private void LoadLayoutFromString()
{
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(this.dockingLayoutXml)))
{
var xmlDocument = XElement.Load(stream);
List<XElement> nodeList = xmlDocument.Descendants("RadPane").ToList();
var paneTags = nodeList.Attributes().Where(a => a.Name == "SerializationTag").Select(a => a.Value).ToList();
var viewModel = this.DataContext as ViewModel;
var newPanes = viewModel.PaneViewModels.Where(paneViewModel => !paneTags.Contains(paneViewModel.SerializationTag)).ToList();
foreach (var item in newPanes)
{
viewModel.PaneViewModels.Remove(item);
}
stream.Seek(0, SeekOrigin.Begin);
this.Docking.LoadLayout(stream);
foreach (var item in newPanes)
{
viewModel.PaneViewModels.Add(item);
}
}
}
private void OnDockingLoaded(object sender, RoutedEventArgs e)
{
this.LoadLayoutFromString();
}
}
}