我创建了一个应用程序,其中有一系列命令绑定在我的应用程序的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. 作为包含DockableContent的新窗口控制
  3. 作为浮动风,是通过通过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 ...但是该按钮仍被禁用。

有帮助吗?

解决方案

解决方案是,当仍调用MainWindow的构造函数时,未设置命令键盘绑定为null -application.current.mainwindow。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top