Prism自定义命令在xaml中没有显示

本文关键字:显示 xaml 自定义 命令 Prism | 更新日期: 2023-09-27 18:13:33

我有一个自定义的命令定义在我的项目编译良好。当我尝试从xaml视图中使用它时,它没有显示出来。下面是代码。我在我的xaml中引用了我的本地项目,所以我不确定发生了什么。

public class MouseDownCommandBehavior : CommandBehaviorBase<TextBox>
{
    public MouseDownCommandBehavior(TextBox element)
        : base(element)
    {
        element.MouseDown += new MouseButtonEventHandler(element_MouseDown);
    }
    private void element_MouseDown(object s, MouseEventArgs e)
    {
        base.ExecuteCommand();
    }
}
public static class MouseDown
{
    public static readonly DependencyProperty BehaviorProperty =
        DependencyProperty.RegisterAttached(
            "MouseDownCommandBehavior",
            typeof(MouseDownCommandBehavior),
            typeof(TextBox),
            null);
    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
        "Command",
        typeof(ICommand),
        typeof(MouseDown),
        new PropertyMetadata(OnSetCommand));
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached(
            "CommandParameter",
            typeof(object),
            typeof(MouseDown),
            new PropertyMetadata(OnSetParameter));
    private static void OnSetCommand(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        TextBox element = dependencyObject as TextBox;
        if (element != null)
        {
            MouseDownCommandBehavior behavior = GetOrCreateBehavior(element);
            behavior.Command = e.NewValue as ICommand;
        }
    }
    private static void OnSetParameter(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        TextBox element = dependencyObject as TextBox;
        if (element != null)
        {
            MouseDownCommandBehavior behavior = GetOrCreateBehavior(element);
            behavior.CommandParameter = e.NewValue;
        }
    }
    private static MouseDownCommandBehavior GetOrCreateBehavior(TextBox element)
    {
        MouseDownCommandBehavior behavior = element.GetValue(BehaviorProperty) as MouseDownCommandBehavior;
        if (behavior == null)
        {
            behavior = new MouseDownCommandBehavior(element);
            element.SetValue(BehaviorProperty, behavior);
        }
        return behavior;
    }
}

Prism自定义命令在xaml中没有显示

原来我错过了几个方法:GetCommand, SetCommand, SetCommandParameter,一个GetCommandParameter