AvalonDock 中的命令绑定问题
我创建了一个应用程序,其中有一系列命令绑定附加到我的应用程序的 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 种情况可以使用此命令称为:
- 作为应用程序主窗口中的 DockableContent
- 作为新的 Window 控件,包含 DockableContent
- 作为 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:
- As a DockableContent within the application's main window
- As a new Window control, containing the DockableContent
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是 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.