司令部AvalonDock
问题描述:
绑定问题
我已经创建了一个应用程序,其中有连接到我的应用程序的主窗口的一系列命令绑定:司令部AvalonDock
(代码简化为简洁起见)
<Window x:Class="DBBrowser.Design.Project.ProjectView"
...>
<Window.CommandBindings>
<Commands:DataContextCommandBinding Command="ProjectCommands:ProjectRoutedCommands.OpenReferenceList" Executed="OpenReferenceList" CanExecute="CanOpenReferenceList"/>
...
</Window.CommandBindings>
</Window>
在该项目的视图模型是两个功能:
public bool CanOpenReferenceList(object parameter)
{
return true;
}
public void OpenReferenceList(object parameter)
{
var dockedReferenceList = new DockableUniversalListView()
{
Name = "referenceList",
Title = "Reference List"
};
referenceData = dockedReferenceList.DataContext as ReferenceListViewModel;
if (referenceData != null) referenceData.EvListSelected += WoWObjectListRecieved;
DockedWindows.Add(dockedReferenceList);
}
跳过一堆的细节,有3个场景中该命令可以被称为:
- 作为应用程序的主窗口
- 作为一个新的窗口控制内的DockableContent,含有DockableContent
- 作为FloatingWindow,通过经由AvalonDock
场景#“撕开”的窗口中创建1和#2完美地使用以下命令绑定工作:
<Button Margin="2" Content="Validate" Height="23" Name="Validate" Width="75"
Command="ProjectCommands:ProjectRoutedCommands.OpenReferenceList"
CommandTarget="{Binding Path=MainWindow.DataContext,Source={x:Static Application.Current}}"
DockPanel.Dock="Left"
CommandParameter="{Binding Path=SelectedWoWObjectList}"
TabIndex="20" HorizontalAlignment="Right"/>
但是,当我“撕掉”A valonDock窗口,按钮灰色。但是,堆栈跟踪显示CanExecute()正在被调用,并且该按钮返回true,但Button仍处于禁用状态。
答
解决方法是CommandTarget绑定为null - 当MainWindow的构造函数仍在调用时,Application.Current.MainWindow未设置。