我已将 AvalonDock 2.0 集成到我的应用程序中。我已将文档和可锚定源绑定到我的视图模型,这些视图模型通过DataTemplate
s 使用适当的用户控件呈现。
我可以使用XmlLayoutSerializer
. 我需要支持按需加载预定义的布局(通过Button
s 和ICommand
s)。这也有效。
我无法工作的事情DockingManager
是在加载完视图模型及其视图(用户控件)后自动加载序列化布局。
我已经尝试加载我的布局,DockingManager.DataContextChanged
但我认为它触发得太早了,因为布局加载了隐藏部分中的文档和可见部分中的重复文档。渲染的窗格不反映加载的布局,当再次保存布局时,重复项会累积在隐藏部分中。
<ad:DockingManager Name="DockingManager"
DataContext="{Binding Project}"
DataContextChanged="DockingManager_OnDataContextChanged"
ActiveContent="{Binding Active}"
AnchorablesSource="{Binding Anchorables}"
DocumentsSource="{Binding Documents}">
<ad:DockingManager.Theme>
<ad:AeroTheme/>
</ad:DockingManager.Theme>
<ad:DockingManager.LayoutItemTemplateSelector>
<views:PanesTemplateSelector>
<views:PanesTemplateSelector.ChartTemplate>
<DataTemplate>
<views:Chart/>
</DataTemplate>
</views:PanesTemplateSelector.ChartTemplate>
<views:PanesTemplateSelector.WorkspaceTemplate>
<DataTemplate>
<Views:Workspace/>
</DataTemplate>
</views:PanesTemplateSelector.WorkspaceTemplate>
...
</views:PanesTemplateSelector>
</ad:DockingManager.LayoutItemTemplateSelector>
<ad:DockingManager.LayoutItemContainerStyle>
<Style TargetType="{x:Type ad:LayoutItem}">
<Setter Property="Title" Value="{Binding Model.DisplayText}"/>
<Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
</Style>
</ad:DockingManager.LayoutItemContainerStyle>
<ad:LayoutRoot>
<!--<ad:LayoutPanel>
<ad:LayoutDocumentPane/>
<ad:LayoutAnchorablePane/>
</ad:LayoutPanel>-->
</ad:LayoutRoot>
</ad:DockingManager>
...和代码隐藏...
private void SaveLayout() {
if (this.DataContext == null)
return;
var xmlLayoutSerializer = new XmlLayoutSerializer(this.DockingManager);
var stringBuilder = new StringBuilder();
using (var textWriter = new StringWriter(stringBuilder))
xmlLayoutSerializer.Serialize(textWriter);
var serialized = stringBuilder.ToString();
(this.DataContext as dynamic).XmlSerializedAndEscapedLayout = HttpUtility.HtmlEncode(serialized);
}
private void LoadLayout()
{
if (DataContext == null)
return;
var encoded = (DataContext as dynamic).XmlSerializedAndEscapedLayout;
var window = Window.GetWindow(this);
window.Closing += (sender, args) => SaveLayout();
if (String.IsNullOrWhiteSpace(encoded))
return;
var serialized = HttpUtility.HtmlDecode(encoded);
var xmlLayoutSerializer = new XmlLayoutSerializer(DockingManager);
using (var stringReader = new StringReader(serialized))
xmlLayoutSerializer.Deserialize(stringReader);
}
private void DockingManager_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null) // A type check here would be best, I know.
LoadLayout();
}
...和视图模型...
public class ProjectViewModel
{
public IModel Model { get; private set; }
public String SerializedLayout { get; set; }
public ViewModelBase Active { get; set; }
private readonly ObservableCollection<ViewModelBase> documents;
public ReadOnlyObservableCollection<ViewModelBase> Documents { get; private set; }
private readonly ObservableCollection<ViewModelBase> anchorables;
public ReadOnlyObservableCollection<ViewModelBase> Anchorables { get; private set; }
public ProjectViewModel(String filePath, String serializedLayout)
{
SerializedLayout = serializedLayout;
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
IModelRepository modelRepository = Ioc.DependencyInjectionContainer.DefaultContainer.Resolve<IModelRepository>();
Model = modelRepository.Load(fileStream);
}
documents = new ObservableCollection<ViewModelBase>();
anchorables = new ObservableCollection<ViewModelBase>();
documents.Add(new Workspace());
anchorables.Add(new RiskLimitsViewModel(Model.RiskLimits));
...
Documents = new ReadOnlyObservableCollection<ViewModelBase>(documents);
Anchorables = new ReadOnlyObservableCollection<ViewModelBase>(anchorables);
}
}
任何见解将不胜感激。谢谢!