【问题标题】:Command Binding issues in AvalonDockAvalonDock 中的命令绑定问题
【发布时间】:2010-11-10 20:06:31
【问题描述】:

我创建了一个应用程序,其中有一系列命令绑定附加到我的应用程序的 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 仍处于禁用状态。

【问题讨论】:

    标签: wpf floating avalondock commandbinding canexecute


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2015-11-17
      • 2011-06-08
      • 2021-07-19
      • 2011-07-02
      相关资源
      最近更新 更多