提问人:ˈvɔlə 提问时间:8/16/2016 最后编辑:ˈvɔlə 更新时间:5/24/2018 访问量:1703
加载和保存可锚点的布局 - 可见性绑定
Load and save layout of anchorables - Binding of Visibility
问:
我面临的问题是,在加载旧布局后,我无法打开X类型的锚点。只有当我在保存布局之前关闭了 X 类型的可定位对象时,才会发生这种情况。
有没有人对 AvalonDock 有类似的问题?这是 AvalonDock 的错误吗?经过多年的调试,我担心在 ViewModel 中更改时,绑定无法在视图中正确更新。AvalonDock 应该负责这项任务。但也许问题在于布局的加载和保存?<Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/>
IsActive
代码
视图
我正在我的视图中加载我的 DockingManager
事件中保存的可锚点(= 工具窗口)布局,如下所示(简化):Loaded
string savedLayout = Properties.Settings.Default.Layout;
XmlDocument doc = new XmlDocument();
doc.LoadXml(savedLayout);
// very simplified code. load saved xml layout and add anchorables to the dockmanager
doc.SelectNodes("//LayoutAnchorable").OfType<XmlNode>().ToList().ForEach(anchorable =>
{
this.DockManager.AnchorablesSource.Add(anchorable);
});
我在我的视图中将我的 MainWindow
的 Closing
Event 中的锚点的当前布局保存为这样(简化):
XmlDocument doc = new XmlDocument();
XmlLayoutSerializer xmlLayoutSerializer = new XmlLayoutSerializer(this.DockManager);
using (MemoryStream stream = new MemoryStream())
{
xmlLayoutSerializer.Serialize(stream);
stream.Seek(0, SeekOrigin.Begin);
doc.Load(stream);
}
// here happens some magic. i think this code is not responsible for my problem
Properties.Settings.Default.Layout = doc.OuterXml;
ViewModel 绑定到 XAML 中的 ViewModel,如下所示(简化):
<xcad:DockingManager x:Name="DockManager" AnchorablesSource="{Binding Tools}" Loaded="DockManager_Loaded">
<xcad:DockingManager.LayoutItemContainerStyle>
<Style TargetType="{x:Type dockctrl:LayoutItem}">
<Setter Property="Title" Value="{Binding Model.ContentId}" />
<Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}" />
<Setter Property="CanClose" Value="{Binding Model.CanClose, Mode=TwoWay}" />
<Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource Bool2vis}, ConverterParameter={x:Static Visibility.Hidden}}"/>
<Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
<Setter Property="IconSource" Value="{Binding Model.IconSource}" />
<Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/>
<Setter Property="ContentId" Value="{Binding Model.ContentId}" />
</Style>
</xcad:DockingManager.LayoutItemContainerStyle>
[...]
视图模型
定位点在 MainWindow 的 ViewModel 中打开。以下是消息的示例代码:
public ObservableCollection<ToolBoxViewModelBase> Tools { get; } = new ObservableCollection<ToolBoxViewModelBase>();
public MainWindowViewModel()
{
// [...]
this.MessagesWindow = new MessagesWindowViewModel();
SimpleIoc.Default.Register<MessagesWindowViewModel>(() => this.MessagesWindow);
this.ShowMessagesWindowCommand = new RelayCommand(() => this.OpenToolBox(this.MessagesWindow));
// [...]
}
public void OpenToolBox<T>(T viewModel) where T : ToolBoxViewModelBase
{
// [...]
viewModel.IsVisible = true;
viewModel.IsActive = true;
// [...]
}
如果您需要更多信息,或者我是否错过了添加一些代码,请告诉我!
答:
0赞
trix
5/24/2018
#1
也许我误解了你的问题,但是...... 属性不用于打开保存到布局中的工具。该属性用于将工具设置为活动(聚焦)。为了打开保存到布局中的工具,您应该处理附加的
像这样的东西:IsActive
layoutSerializer_LayoutSerializationCallback
var layoutSerializer = new XmlLayoutSerializer(this.DockManager);
layoutSerializer.LayoutSerializationCallback += layoutSerializer_LayoutSerializationCallback;
protected virtual void layoutSerializer_LayoutSerializationCallback(object sender, LayoutSerializationCallbackEventArgs e)
{
try
{
var model = this.Docs.Union(this.Tools).FirstOrDefault(vm => vm.ContentId == e.Model.ContentId);
if (model != null)
{
e.Content = model;
}
else
{
// Log load layout error info
}
}
catch (Exception ex)
{
// Log load layout error info
}
}
评论