棱镜定制命令不会出现在xaml中

时间:2011-11-02 19:18:25

标签: c# wpf prism

我的项目中有一个自定义命令定义编译得很好。当我尝试从我的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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

相关问题