本文介绍了如何将ValueConverter应用于基于约定的Caliburn.Micro绑定示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到以下问题:.

I have seen the following question: how-do-you-apply-a-valueconverter-to-a-convention-based-caliburn-micro-binding.

我无法对该主题发表评论,所以我在这里发表我的问题.

I couldn't post a comment on that topic, so I am posting my question here.

如何使用 ConventionManager.使用基于约定的绑定时,Caliburn.Micro中的ApplyValueConverter 用于值转换器?

How to use the ConventionManager.ApplyValueConverter in Caliburn.Micro for value converters when using convention based binding ?

有人可以在这里写一个例子吗?

Could anyone write an example here?

推荐答案

ApplyValueConverter ConventionManager 中定义为静态的 Func<> 委托.code>类.

ApplyValueConverter is defined as a static Func<> delegate in the ConventionManager class.

为了在约定绑定的情况下提供自己的转换器,您需要在引导程序的 Configure()方法中定义自己的 Func<> ,像这样的东西:

In order to provide your own converter in convention-binding scenarios, you need to define your own Func<> in the Configure() method of your bootstrapper, something like this:

注意::我假设转换是从 string Opacity .

NOTE: I am assuming the conversion is from string to Opacity.

public class AppBootstrapper : Bootstrapper<ShellViewModel> {

    private static IValueConverter StringToOpacityConverter = new StringToOpacityConverter();

    public override void Configure() {

        var oldApplyConverterFunc = ConventionManager.ApplyValueConverter;

        ConventionManager.ApplyValueConverter = (binding, bindableProperty, property) => {
            if (bindableProperty == UIElement.Opacity && typeof(string).IsAssignableFrom(property.PropertyType))
            //                                ^^^^^^^           ^^^^^^
            //                             Property in XAML     Property in view-model
                binding.Converter = StringToOpacityConverter;
                //                  ^^^^^^^^^^^^^^^^^^^^^^^^^
                //                 Our converter used here.

            // else we use the default converter
            else
                oldApplyConverterFunc(binding, bindableProperty, property);

        };
    }

}

这篇关于如何将ValueConverter应用于基于约定的Caliburn.Micro绑定示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:43