我有两个WPF项目,一个是.Net 4.7 dll,另一个是桌面应用程序。
在我的dll中,我有课程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
命令始终求值为true。在大多数情况下,ZoomFit
命令也会评估为true(并且两个函数都被调用,我已经检查过了。)
在我的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
类(.Net的一部分)。
上述代码段中的两个按钮始终处于禁用状态,但应启用它们。但是,使用两个缩放命令进行缩放都可以正常工作。
我在这里做错了什么?
答案 0 :(得分:1)
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()
{
}
}