简体   繁体   English

在WPF中,如何实现ICommandSource以使我的自定义控制能力能够使用来自xaml的命令?

[英]In WPF how do I implement ICommandSource to give my custom control ability to use Command from xaml?

Could you please provide a sample, of how do you implement the ICommandSource interface. 能否请您提供一个示例,了解如何实现ICommandSource接口。 As I want my UserControl , which doesn't have the ability to Specify command in xaml, to have this ability. 因为我想要我的UserControl ,它不具备在xaml中指定命令的能力,以具备此功能。 And to be able to handle the command when user clicks on the CustomControl . 并且能够在用户单击CustomControl时处理该命令。

Here's an example : 这是一个例子:

public partial class MyUserControl : UserControl, ICommandSource
{
    public MyUserControl()
    {
        InitializeComponent();
    }



    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(MyUserControl), new UIPropertyMetadata(null));


    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(MyUserControl), new UIPropertyMetadata(null));

    public IInputElement CommandTarget
    {
        get { return (IInputElement)GetValue(CommandTargetProperty); }
        set { SetValue(CommandTargetProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CommandTarget.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandTargetProperty =
        DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(MyUserControl), new UIPropertyMetadata(null));


    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonUp(e);

        var command = Command;
        var parameter = CommandParameter;
        var target = CommandTarget;

        var routedCmd = command as RoutedCommand;
        if (routedCmd != null && routedCmd.CanExecute(parameter, target))
        {
            routedCmd.Execute(parameter, target);
        }
        else if (command != null && command.CanExecute(parameter))
        {
            command.Execute(parameter);
        }
    }

}

Note that the CommandTarget property is only used for RoutedCommands 请注意, CommandTarget属性仅用于RoutedCommands

您的UserControl将在文件cs或vb后面有一个代码,您必须实现接口ICommandSource,一旦实现它,在某些情况下您将必须实际调用该命令并检查CanExecute。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个项目中使用自定义WPF / XAML控件? - How do I use a custom WPF/XAML control in another project? 使用WPF自定义控件,如何通过后面的代码为自定义控件提供名称以访问它? - Using a WPF Custom Control, How can I give my custom control a name to access it via the code behind? 如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件? - How can I make WPF instantiate a custom control in my view, using another custom control base class in my XAML? 如何在XAML模板中使用WPF自定义控件属性? - How to use WPF custom control properties in XAML Template? WPF Smith Html编辑器,如何在xaml中绑定到此控件? - WPF Smith Html Editor, how do I bind to this control in xaml? 如何在XAML WPF中访问自定义控件的属性 - how to access property of a custom control in XAML WPF xaml(wpf) 如何从我的自定义窗口模板中寻址控件? - xaml(wpf) How to address a control from my customwindow template? WPF:在不破坏IsEnabled的情况下实现ICommandSource - WPF: Implement ICommandSource Without Breaking IsEnabled WPF-如何从C#中的XAML文件中获取scrollviewer控件? - WPF - How could I get a scrollviewer control from my XAML file in C#? WPF自定义控件的XAML集合初始化 - XAML collection initialization from WPF custom control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM