在 WPF 用户控件中绑定到路由事件的命令 [英] Command Binding to Routed Event in WPF User-control

查看:55
本文介绍了在 WPF 用户控件中绑定到路由事件的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Viewmodel 命令绑定到用户控件的路由事件.这是我所拥有的详细说明.

I want to bind Viewmodel command to Usercontrol's Routed Event. Here is the detailed explanation of what I have.

我有一个用户控件,它有一个 Image(显示图像)和一个 Button 底部(Button 删除 图片).我在 ListView 中使用用户控件.

I have a User Control which have one Image (which shows image) and one Button at bottom (Button to remove Image). I am using a Usercontrol in a ListView.

在我后面的用户控件代码中,我有一个 RoutedEventHandler 来删除 Image:

In my Usercontrol's Code behind I have a RoutedEventHandler to remove the Image:

public event RoutedEventHandler RemoveImage;

在我使用这个用户控件的窗口中,我放了:

In the window where I use this Usercontrol, I have put:

<uc:ucImageListItem x:Name="ImageListItem" RemoveImage="ImageListItem_RemoveImage"  />

如果我的删除图像的代码在后面的代码中,则此代码可以正常工作.但我想将 Viewmodel 的命令绑定到 RemoveImage RoutedEvent.

This code works correctly if My code to remove image is in code behind. but I want to Bind command of Viewmodel to RemoveImage RoutedEvent.

可能喜欢(不正确)

<uc:ucImageListItem x:Name="ImageListItem" RemoveImage="{binding CommandtoRemove}"  />

如何实现这一目标?

我发现了一些与 RoutedCommandDependancyProperty 相关的东西,但找不到任何正确的方法,如何使用它们.

I found something related to RoutedCommand or DependancyProperty, but could not find any proper way, How to use them.

如果我需要进一步澄清我的问题,请告诉我.感谢期待.

Let me know if I need to further clear my question. Thanks in anticipation.

推荐答案

这段代码展示了如何调用命令:命令处理程序

Hi this piece of code shows how to call command: Command handler

public class CommandHandler : ICommand
{
    public CommandHandler(Action<object> action,Func<object,bool> canexecute)
    {
        _action = action;
        _canExecute = canexecute;

    }
    Action<object> _action;
    Func<object, bool> _canExecute;

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

    public event EventHandler CanExecuteChanged;

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

视图模型

public class MainViewModel
{
    private CommandHandler _buttonCommand;

    public CommandHandler ButtonCommand
    {
        get
        {
            return _buttonCommand ?? (_buttonCommand = new CommandHandler((param) => OnButtonCommand(param),(param)=>true));
        }
    }

    private void OnButtonCommand(object obj)
    {
        //DO things here whatever you want to do on Button click
    } 
}

查看

<Button Command="{Binding ButtonCommand}" Content="ok"/>

您需要向 CommandHandler 构造函数传递两个参数,一个是要在 Command 上触发的 Action,第二个参数是必须返回 bool 的 func.如果 func 评估为真,则命令的动作被触发.并且动作中的参数和 func 是你将绑定到 CommandParameter 在我上面的例子中它将为空,因为我没有绑定 CommandParameter.我希望这会有所帮助.

you need to pass two parameters to CommandHandler Constructor one is Action that you want to fire on Command and second param is func that must return bool. If func evaluates to true only then the Action of Command is fired.And the param in action and func is what you will bind to the CommandParameter in my case above it will be null as I havent binded the CommandParameter.I hope this will help.

这篇关于在 WPF 用户控件中绑定到路由事件的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆