简体   繁体   English

尽管ICommand.CanExecute计算为true,但按钮已禁用

[英]Button disabled although ICommand.CanExecute evaluates to true

I have two WPF projects, one is a .Net 4.7 dll and the other is a desktop app. 我有两个WPF项目,一个是.Net 4.7 dll,另一个是桌面应用程序。

In my dll I have the classes Viewport2D and ViewportCommands 在我的dll中,我有Viewport2DViewportCommands

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

My Viewport2D binds those commands in the ctor: 我的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));

        ...
    }

As you can see the ResetView command always evaluates to true. 如您所见, ResetView命令始终求值为true。 The ZoomFit command evaluates also to true in the most cases (and both functions are called, I checked that already). 在大多数情况下, ZoomFit命令的求值结果也为true(并且两个函数都已调用,我已经检查过)。

In my WPF app I use this control: 在我的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" />

The IncreaseZoom and DecreaseZoom are from the NavigationCommands class (part of .Net). IncreaseZoomDecreaseZoom来自NavigationCommands类(.Net的一部分)。

Both buttons from the snippet above are always disabled although they should be enabled. 尽管上面的代码片段中的两个按钮都应启用,但它们始终被禁用。 However zooming with both zoom commands works just fine. 但是,使用两个缩放命令进行缩放都可以。

What am I doing wrong here? 我在这里做错了什么?

i do a test if the binding is in a user control it does not work; 如果绑定在用户控件中,我将进行测试; if i change to MainWindow, it works...same xaml for both ----GOOD---- 如果我更改为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));
    }

----BAD---- - - 坏 - -

    <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()
        {

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM