简体   繁体   中英

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.

In my dll I have the classes Viewport2D and 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));
}

My Viewport2D binds those commands in the 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. The ZoomFit command evaluates also to true in the most cases (and both functions are called, I checked that already).

In my WPF app I use this control:

  <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).

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

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

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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