首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何查找ICommand绑定目标(控件)

如何查找ICommand绑定目标(控件)
EN

Stack Overflow用户
提问于 2011-10-14 09:53:02
回答 2查看 675关注 0票数 0

我们使用MVVM模式。在视图中,我将save命令绑定到一个按钮:

在视图模型中,我想找出save命令绑定目标,可以吗?

代码语言:javascript
复制
    private Button GetBindingControl(ICommand command)
    {
        // What should I do here:

        return button;
    }
EN

回答 2

Stack Overflow用户

发布于 2011-10-14 10:25:24

这是不可能的,而且它违背了MVVM的目的(将UI逻辑放在VM中,而不管使用的控件是什么)

也许你可以问你想解决的是什么问题。

票数 1
EN

Stack Overflow用户

发布于 2011-10-14 13:33:12

正如@Diego所说,这违背了MVVM的目的,因为我们必须努力不在MVVM的视图模型中包含视觉效果或控件……

话虽如此,有两种选择……

使用RoutedCommands

  • Using附加行为的

RoutedCommands在MVVM中是不被允许的,因为它们需要被紧密的命令绑定到UI元素,也就是我们的例子中的按钮。因此,它们也违背了MVVM的目的。

但MVVM与附加的行为愉快地共存。

许多开发人员都回避这个极其强大的功能。我们可以将其与RoutedCommands一起使用。

在你的情况下

附加到按钮,使用action delegate.

  • Attach string object as
  1. parameter.
  2. Inside behavior,使用一些路由的命令设置Button.Command。
    1. 在executed事件处理程序中,从发送者/
    2. /Button.Command获取按钮操作委托作为按钮,然后使用e.Parameter字符串值相应地调用您的源。

下面是示例代码...

假设您有常用的signature Action<Button, string>按钮实用程序

代码语言:javascript
复制
public static class ButtonActionUtilities
{
    public static Action<Button, string> ButtonActionDelegate
    {
        get
        {
            return ExecuteButtonClick;
        }
    }

    public static void ExecuteButtonClick(Button btn, string param)
    {
        MessageBox.Show(
          "You clicked button " + btn.Content + " with parameter " + param);
    }
}

那么附加的行为如下所示。

代码语言:javascript
复制
public static class ButtonAttachedBehavior
{
    public static readonly DependencyProperty ActionDelegateProperty
        = DependencyProperty.RegisterAttached(
            "ActionDelegate",
            typeof(Action<Button, string>),
            typeof(ButtonAttachedBehavior),
            new PropertyMetadata(null, OnActionDelegatePropertyChanged));

    public static Action<Button, string> GetActionDelegate(
                                            DependencyObject depObj)
    {
        return (Action<Button, string>)depObj.GetValue(
                                            ActionDelegateProperty);
    }

    public static void SetActionDelegate(
         DependencyObject depObj, Action<Button, string> value)
    {
        depObj.SetValue(ActionDelegateProperty, value);
    }

    private static void OnActionDelegatePropertyChanged(
        DependencyObject depObj,
        DependencyPropertyChangedEventArgs e)
    {
        if (depObj is Button
            && e.NewValue is Action<Button, string>)
        {               
            ((Button)depObj).Command
                = new RoutedCommand(
                        "ActionRoutedCommand",
                         typeof(ButtonAttachedBehavior));
            ((Button) depObj).CommandBindings.Add(
                new CommandBinding(
                    ((Button) depObj).Command,
                    OnActionRoutedCommandExecuted));
        }
    }

    private static void OnActionRoutedCommandExecuted(
       object sender, ExecutedRoutedEventArgs e)
    {
        var actionDelegate = GetActionDelegate((Button)e.Source);
        actionDelegate((Button) e.Source, (string)e.Parameter);
    }
}

在XAML上,它将看起来像这样……

代码语言:javascript
复制
      <StackPanel>
         <Button x:Name="TestButton" Content="Test Me" 
            local:ButtonAttachedBehavior.ActionDelegate
                ="{x:Static local:ButtonActionUtilities.ButtonActionDelegate}"
            CommandParameter
                ="{Binding Text, ElementName=ParameterTextBox}"/>

          <TextBox x:Name="ParameterTextBox"/>
      </StackPanel>

因此,使用上面的代码,您只需要将ActionDelegate attached属性设置为approapriate委托,它就会执行该委托。

我仍然建议您修改现有的代码设置,以分离特定于按钮的行为,使其更加友好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7762331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档