我已经集成AvalonDock 2.0到我的应用程序。 我绑定的文件和锚能源到我的视图模型被渲染通过适当的用户控件DataTemplate
秒。
我可以加载和保存布局与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);
}
}
任何有识之士将不胜感激。 谢谢!