尽管 ICommand.CanExecute 的计算结果为真,但按钮已禁用
Button disabled although ICommand.CanExecute evaluates to true
我有两个 WPF 项目,一个是 .Net 4.7 dll,另一个是桌面应用程序。
在我的 dll 中我有 classes Viewport2D
和 ViewportCommands
public static class ViewportCommands
{
public static RoutedCommand ResetView { get; } = new RoutedCommand(nameof(ResetView), typeof(ViewportCommands));
public static RoutedCommand ZoomFit { get; } = new RoutedCommand(nameof(ZoomFit), typeof(ViewportCommands));
}
我的 Viewport2D 在 ctor 中绑定了那些命令:
public Viewport2D()
{
...
CommandBindings.Add(new CommandBinding(NavigationCommands.IncreaseZoom, ExecuteZoomIn, CanZoom));
CommandBindings.Add(new CommandBinding(NavigationCommands.DecreaseZoom, ExecuteZoomOut, CanZoom));
CommandBindings.Add(new CommandBinding(ViewportCommands.ZoomFit, (o, e) => ZoomFit(), CanZoom));
CommandBindings.Add(new CommandBinding(ViewportCommands.ResetView, (o, e) => Reset(), (o, e) => e.CanExecute = true));
...
}
如您所见,ResetView
命令的计算结果始终为真。在大多数情况下,ZoomFit
命令的计算结果也为真(并且两个函数都被调用,我已经检查过了)。
在我的 WPF 应用程序中,我使用了这个控件:
<controls1:Viewport2D x:Name="Zoombox" ...>
<controls1:Viewport2D.InputBindings>
<MouseBinding Gesture="{wpfUtils:MouseWheel Direction=Down}" Command="DecreaseZoom" CommandTarget="{Binding ElementName=Zoombox}" />
<MouseBinding Gesture="{wpfUtils:MouseWheel Direction=Up}" Command="IncreaseZoom" CommandTarget="{Binding ElementName=Zoombox}" />
</controls1:Viewport2D.InputBindings>
...
</controls1:Viewport2D>
...
<Button Content="100%" Width="70" Command="controls1:ViewportCommands.ResetView" />
<Button Content="Zoom Fit" Width="70" Command="controls1:ViewportCommands.ZoomFit" />
IncreaseZoom
和 DecreaseZoom
来自 NavigationCommands
class(.Net 的一部分)。
上面代码片段中的两个按钮始终处于禁用状态,尽管它们应该处于启用状态。但是,使用两个缩放命令进行缩放都可以正常工作。
我做错了什么?
我做个测试
如果绑定在用户控件中,则它不起作用;如果我更改为 MainWindow,它会起作用...两者都相同 xaml
----好----
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ViewportCommands.ZoomFit, (o, e) => ZoomFit(), (o, e) => e.CanExecute = true));
CommandBindings.Add(new CommandBinding(ViewportCommands.ResetView, (o, e) => Reset(), (o, e) => e.CanExecute = true));
}
public void ZoomFit()
{
}
public void Reset()
{
}
}
public static class ViewportCommands
{
public static RoutedCommand ResetView { get; } = new RoutedCommand(nameof(ResetView), typeof(ViewportCommands));
public static RoutedCommand ZoomFit { get; } = new RoutedCommand(nameof(ZoomFit), typeof(ViewportCommands));
}
----不好----
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:UserControl1></local:UserControl1>
<Button Content="100%" Width="70" Command="local:ViewportCommands.ResetView" />
<Button Content="Zoom Fit" Width="70" Command="local:ViewportCommands.ZoomFit" />
</StackPanel>
</Window>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ViewportCommands.ZoomFit, (o, e) => ZoomFit(), (o, e) => e.CanExecute = true));
CommandBindings.Add(new CommandBinding(ViewportCommands.ResetView, (o, e) => Reset(), (o, e) => e.CanExecute = true));
}
public void ZoomFit()
{
}
public void Reset()
{
}
}
我有两个 WPF 项目,一个是 .Net 4.7 dll,另一个是桌面应用程序。
在我的 dll 中我有 classes Viewport2D
和 ViewportCommands
public static class ViewportCommands
{
public static RoutedCommand ResetView { get; } = new RoutedCommand(nameof(ResetView), typeof(ViewportCommands));
public static RoutedCommand ZoomFit { get; } = new RoutedCommand(nameof(ZoomFit), typeof(ViewportCommands));
}
我的 Viewport2D 在 ctor 中绑定了那些命令:
public Viewport2D()
{
...
CommandBindings.Add(new CommandBinding(NavigationCommands.IncreaseZoom, ExecuteZoomIn, CanZoom));
CommandBindings.Add(new CommandBinding(NavigationCommands.DecreaseZoom, ExecuteZoomOut, CanZoom));
CommandBindings.Add(new CommandBinding(ViewportCommands.ZoomFit, (o, e) => ZoomFit(), CanZoom));
CommandBindings.Add(new CommandBinding(ViewportCommands.ResetView, (o, e) => Reset(), (o, e) => e.CanExecute = true));
...
}
如您所见,ResetView
命令的计算结果始终为真。在大多数情况下,ZoomFit
命令的计算结果也为真(并且两个函数都被调用,我已经检查过了)。
在我的 WPF 应用程序中,我使用了这个控件:
<controls1:Viewport2D x:Name="Zoombox" ...>
<controls1:Viewport2D.InputBindings>
<MouseBinding Gesture="{wpfUtils:MouseWheel Direction=Down}" Command="DecreaseZoom" CommandTarget="{Binding ElementName=Zoombox}" />
<MouseBinding Gesture="{wpfUtils:MouseWheel Direction=Up}" Command="IncreaseZoom" CommandTarget="{Binding ElementName=Zoombox}" />
</controls1:Viewport2D.InputBindings>
...
</controls1:Viewport2D>
...
<Button Content="100%" Width="70" Command="controls1:ViewportCommands.ResetView" />
<Button Content="Zoom Fit" Width="70" Command="controls1:ViewportCommands.ZoomFit" />
IncreaseZoom
和 DecreaseZoom
来自 NavigationCommands
class(.Net 的一部分)。
上面代码片段中的两个按钮始终处于禁用状态,尽管它们应该处于启用状态。但是,使用两个缩放命令进行缩放都可以正常工作。
我做错了什么?
我做个测试 如果绑定在用户控件中,则它不起作用;如果我更改为 MainWindow,它会起作用...两者都相同 xaml ----好----
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ViewportCommands.ZoomFit, (o, e) => ZoomFit(), (o, e) => e.CanExecute = true));
CommandBindings.Add(new CommandBinding(ViewportCommands.ResetView, (o, e) => Reset(), (o, e) => e.CanExecute = true));
}
public void ZoomFit()
{
}
public void Reset()
{
}
}
public static class ViewportCommands
{
public static RoutedCommand ResetView { get; } = new RoutedCommand(nameof(ResetView), typeof(ViewportCommands));
public static RoutedCommand ZoomFit { get; } = new RoutedCommand(nameof(ZoomFit), typeof(ViewportCommands));
}
----不好----
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:UserControl1></local:UserControl1>
<Button Content="100%" Width="70" Command="local:ViewportCommands.ResetView" />
<Button Content="Zoom Fit" Width="70" Command="local:ViewportCommands.ZoomFit" />
</StackPanel>
</Window>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ViewportCommands.ZoomFit, (o, e) => ZoomFit(), (o, e) => e.CanExecute = true));
CommandBindings.Add(new CommandBinding(ViewportCommands.ResetView, (o, e) => Reset(), (o, e) => e.CanExecute = true));
}
public void ZoomFit()
{
}
public void Reset()
{
}
}