AvalonDock 中的命令绑定问题

发布于 2024-10-01 19:24:44 字数 1700 浏览 0 评论 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...但该按钮仍处于禁用状态。

I have created an application where there's a series of command bindings attached to the MainWindow of my application:

(Code simplified for brevity)

<Window x:Class="DBBrowser.Design.Project.ProjectView" 
...>

    <Window.CommandBindings>
    <Commands:DataContextCommandBinding Command="ProjectCommands:ProjectRoutedCommands.OpenReferenceList" Executed="OpenReferenceList" CanExecute="CanOpenReferenceList"/>
...
</Window.CommandBindings>
</Window>

Within the project's ViewModel are two functions:

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);
}

Skipping over a bunch of details, there are 3 scenarios where this Command can be called:

  1. As a DockableContent within the application's main window
  2. As a new Window control, containing the DockableContent
  3. As a FloatingWindow, created by "tearing off" the window via AvalonDock

Scenario #1 and #2 work perfectly using the following command binding:

<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"/>

However, when I "Tear off" the AvalonDock window, the button greys out. However, a stack trace showed that CanExecute() was being called AND returning true for that button... but the Button remained disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌能量女王 2024-10-08 19:24:44

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

The solution was that the CommandTarget binding was null - Application.Current.MainWindow is not set when the Constructor for the MainWindow is still being called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文