0

我有一个带有自定义 UserControl 的 WPF 应用程序,我想通过使用 Microsoft.Xaml.Behaviors 库中的简单 EventTrigger 来响应鼠标事件。我还有一个画布,需要显示或隐藏,以反映我的 ViewModel 类中的 SelectedProperty 被更改。当单击鼠标右键时,我的编辑命令被提出并负责在我的 ViewModel 类上将 SelectedProperty 更新为 false。

这是我的委托命令类:

public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public event EventHandler CanExecuteChanged;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute,
        Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public  bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

    public  void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged == null) return;
        CanExecuteChanged(this, EventArgs.Empty);
    }
}

这是我的带有 ICommand 和 SelectedProperty 的 ViewModel 类:

public class ViewModel { 
    public static readonly DependencyProperty SelectedProperty =
        DependencyProperty.Register("Selected", typeof(Boolean), typeof(ViewModel));

    public Boolean Selected
    {
        get => (Boolean) GetValue(SelectedProperty);
        set => SetValue(SelectedProperty, value);
    }
    public static readonly DependencyProperty EditNodeProperty =
        DependencyProperty.Register("EditNode", typeof(ICommand), typeof(ViewModel));

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public ICommand EditNode
    {
        get => (ICommand)GetValue(EditNodeProperty);
        set => SetValue(EditNodeProperty, value);
    }
    public Boolean CanEditNode(object parameter)
    {
        return true;
    }
    public void EditNodeHandler(object parameter)
    {
        Selected = false;
    }
}

这是使用编辑命令的用户控制代码。

<UserControl ...
    xmlns:b="http://schemas.microsoft.com/xaml/behaviors"    
    ...> 
...
     <b:Interaction.Triggers>
        <b:EventTrigger EventName="MouseRightButtonUp">
            <b:InvokeCommandAction Command="{Binding EditNode}"
                CommandParameter="{Binding}"></b:InvokeCommandAction>
        </b:EventTrigger>
    </b:Interaction.Triggers>
...
</UserControl>

还有另一个用户控件,使用该属性来隐藏和显示基于 SelectedProperty 状态的画布:

<UserControl...>
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter1"></BooleanToVisibilityConverter>
    </UserControl.Resources>

    <Canvas Margin="0" Visibility="{Binding Selected, Converter={StaticResource 
         BooleanToVisibilityConverter1}}">
       ...
    </Canvas>
</UserControl>

当我单击鼠标右键时,会执行 EditNodeHandler,并且参数是正确的 ViewModel 实例(选中 == true)。我还可以在 Visual Studio 中设置断点并中断处理程序代码。但是,当在处理程序中将 Selected 属性设置为 false 时,画布不会被隐藏。如果我使用 Snoop 调试器查看 DataContext,我可以手动将 SelectedProperty 设置为 false,并且用户界面会按预期隐藏 Canvas。

那么,问题是为什么命令处理程序没有按预期更新用户界面?

4

0 回答 0