如何将 ValueConverter 应用于基于约定的 Caliburn.Micro 绑定示例?

2024-02-27

我看到了以下问题:如何将值转换器应用到基于约定的 caliburn 微绑定 https://stackoverflow.com/questions/6581674/how-do-you-apply-a-valueconverter-to-a-convention-based-caliburn-micro-binding.

我无法对该主题发表评论,因此我在这里发布我的问题。

如何使用ConventionManager.ApplyValueConverter https://caliburnmicro.codeplex.com/SourceControl/latest#src/Caliburn.Micro.Platform/ConventionManager.cs使用基于约定的绑定时,在 Caliburn.Micro 中进行值转换器?

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


ApplyValueConverter被定义为静态Func<>代表在ConventionManager class.

为了在约定绑定场景中提供自己的转换器,您需要定义自己的转换器Func<> in the Configure()引导程序的方法,如下所示:

NOTE:我假设转换来自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);

        };
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 ValueConverter 应用于基于约定的 Caliburn.Micro 绑定示例? 的相关文章

随机推荐