How do you apply a ValueConverter to a convention-based Caliburn.Micro binding Example?

别等时光非礼了梦想. 提交于 2021-02-07 03:25:18

问题


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.

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

Could anyone write an example here?


回答1:


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

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:

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);

        };
    }

}


来源:https://stackoverflow.com/questions/19175245/how-do-you-apply-a-valueconverter-to-a-convention-based-caliburn-micro-binding-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!