【问题标题】:How do you apply a ValueConverter to a convention-based Caliburn.Micro binding Example?如何将 ValueConverter 应用于基于约定的 Caliburn.Micro 绑定示例?
【发布时间】:2013-10-11 02:22:37
【问题描述】:

我看到了以下问题:how-do-you-apply-a-valueconverter-to-a-convention-based-caliburn-micro-binding

我无法就该主题发表评论,所以我在这里发布我的问题。

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

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

【问题讨论】:

    标签: c# xaml binding windows-phone-8 caliburn.micro


    【解决方案1】:

    ApplyValueConverter 被定义为ConventionManager 类中的静态Func<> 委托。

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

    注意:我假设转换是从stringOpacity

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

    【讨论】:

    • 哇,你知道我已经使用它一段时间了,我什至没有意识到有一个价值转换器的约定!这节省了大量的打字!
    • @Charleh 有趣的是,在 OP 问之前我不知道 :)。我很高兴我也学到了这一点。
    • 这是我讨厌的一件事,但从未想过要检查是否支持...。(大多数)转换器在打字量与收益方面几乎没有什么优势!
    猜你喜欢
    • 2011-09-28
    • 2011-05-14
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多