0

我创建了一个应用程序,其中有一系列命令绑定附加到我的应用程序的 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. 作为应用程序主窗口中的 DockableContent
  2. 作为一个新的 Window 控件,包含 DockableContent
  3. 作为 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"/>

但是,当我“撕下” AvalonDock 窗口时,按钮变灰。但是,堆栈跟踪显示 CanExecute() 被调用并为该按钮返回 true ......但 Button 仍处于禁用状态。

4

1 回答 1

1

解决方案是 CommandTarget 绑定为空 - 当仍在调用 MainWindow 的构造函数时未设置 Application.Current.MainWindow。

于 2010-11-11T03:05:07.983 回答