MVVM:如何对控件进行函数调用? [英] MVVM: How to make a function call on a control?

查看:89
本文介绍了MVVM:如何对控件进行函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XAML 中,我有一个文本框,其中 x:Name 为 MyTextBox.

In XAML, I have a TextBox with x:Name of MyTextBox.

<TextBox x:Name="MyTextBox">Some text</TextBox>

出于速度原因,我想调用方法 .AppendText,例如在后面的 C# 代码中,我会调用 MyTextBox.AppendText("...")

For speed reasons, I want to call the method .AppendText, e.g. In C# code behind, I would call MyTextBox.AppendText("...")

然而,这不是很像 MVVM.如果我想使用绑定到我的 ViewModel 来调用控件上的函数,那么实现此目的的优雅方法是什么?

However, this is not very MVVM like. If I want to make a call to a function on a control using binding to my ViewModel, what is an elegant way to achieve this?

我正在使用 MVVM Light.

I'm using MVVM Light.

如果我想要一个简单、快速的解决方案,我会使用@XAML Lover 的答案.此答案使用了较少 C# 编码的混合行为.

I would use the answer from @XAML Lover if I wanted a simple, quick solution. This answer uses a Blend Behavior which is less C# coding.

如果我想编写一个可重用的依赖属性,我将使用@Chris Eelmaa 的答案,该属性将来可以应用于任何 TextBox.这个例子基于一个依赖属性,虽然稍微复杂一些,但是一旦编写就非常强大和可重用.当它插入本机类型时,使用它的 XAML 也略少.

I would use the answer from @Chris Eelmaa if I wanted write a reusable Dependency Property which I could apply to any TextBox in the future. This example is based on a Dependency Property which, while slightly more complex, is very powerful and reusable once it is written. As it plugs into the native type, there is also slightly less XAML to use it.

推荐答案

基本上当你从一个控件调用一个方法时,很明显你是在做一些 UI 相关的逻辑.这不应该放在 ViewModel 中.但在某些特殊情况下,我建议创建一个行为.创建一个 Behavior 并定义一个 Action 类型的 DependencyProperty,因为 AppendText 应将字符串作为参数.

Basically when you call a method from a control, it is obvious that you are doing some UI related logic. And that should not sit in ViewModel. But in some exceptional case, I would suggest to create a behavior. Create a Behavior and define a DependencyProperty of type Action<string> since AppendText should take string as a parameter.

public class AppendTextBehavior : Behavior<TextBlock>
{
    public Action<string> AppendTextAction
    {
        get { return (Action<string>)GetValue(AppendTextActionProperty); }
        set { SetValue(AppendTextActionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for AppendTextAction.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AppendTextActionProperty =
        DependencyProperty.Register("AppendTextAction", typeof(Action<string>), typeof(AppendTextBehavior), new PropertyMetadata(null));

    protected override void OnAttached()
    {
        SetCurrentValue(AppendTextActionProperty, (Action<string>)AssociatedObject.AppendText);
        base.OnAttached();
    }
}

在 OnAttached 方法中,我已将在 TextBlock 上创建的扩展方法分配给 Behavior 的 DP.现在我们可以将此行为附加到 View 中的 TextBlock.

In the OnAttached method, I have assigned the extension method that I have created on TextBlock to the DP of Behavior. Now we can attach this behavior to a TextBlock in View.

    <TextBlock Text="Original String"
               VerticalAlignment="Top">
        <i:Interaction.Behaviors>
            <wpfApplication1:AppendTextBehavior AppendTextAction="{Binding AppendTextAction, Mode=OneWayToSource}" />
        </i:Interaction.Behaviors>
    </TextBlock>

考虑我们在 ViewModel 中有一个具有相同签名的属性.该属性是此绑定的来源.然后我们可以随时调用该 Action,它会自动调用我们在 TextBlock 上的扩展方法.在这里,我通过单击按钮调用该方法.请记住,在这种情况下,我们的 Behavior 就像 View 和 ViewModel 之间的适配器.

Consider we have a property in ViewModel with same signature. And that property is the source of this binding. Then we can invoke that Action anytime, which will automatically invoke our extension method on TextBlock. Here I am invoking the method on a button click. Remember in this case, our Behavior acts like an Adapter between View and ViewModel.

public class ViewModel
{
    public Action<string> AppendTextAction { get; set; }

    public ICommand ClickCommand { get; set; }

    public ViewModel()
    {
        ClickCommand = new DelegateCommand(OnClick);
    }

    private void OnClick()
    {
        AppendTextAction.Invoke(" test");
    }
}

这篇关于MVVM:如何对控件进行函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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