我创建了一个应用程序,其中有一系列命令绑定附加到我的应用程序的MainWindow:
(为简洁而简化的代码)
<Window x:Class="DBBrowser.Design.Project.ProjectView"
...>
<Window.CommandBindings>
<Commands:DataContextCommandBinding Command="ProjectCommands:ProjectRoutedCommands.OpenReferenceList" Executed="OpenReferenceList" CanExecute="CanOpenReferenceList"/>
...
</Window.CommandBindings>
</Window>
在项目中,ViewModel有两个功能:
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种情况可以调用此命令:
场景#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"/>
但是,当我“撕下”AvalonDock窗口时,按钮会变灰。但是,堆栈跟踪显示正在调用CanExecute()并为该按钮返回true ...但Button仍然处于禁用状态。
答案 0 :(得分:1)
解决方案是CommandTarget绑定为null - 当仍在调用MainWindow的构造函数时,未设置Application.Current.MainWindow。