c# - 如何将 ValueConverter 应用于基于约定的 Caliburn.Micro 绑定(bind)示例?

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

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

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

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

谁能在这里写一个例子?

最佳答案

ApplyValueConverter被定义为静态 Func<>代表 ConventionManager类。

为了在约定绑定(bind)场景中提供自己的转换器,您需要定义自己的 Func<>Configure()你的 Bootstrap 的方法,像这样:

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

        };
    }

}

关于c# - 如何将 ValueConverter 应用于基于约定的 Caliburn.Micro 绑定(bind)示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19175245/

相关文章:

c# - 将 Combobox 控件绑定(bind)到 DataGrid 中的单独源

c# - 我可以将参数从一种方法定义到另一种方法吗

c# - 在 Asp.net 中显示括号

C# - 获取对对象的引用数

c# - 行为 View 模型绑定(bind)

c# - WPF 工具包 - 设置具有绑定(bind)的 Line Series 多段线颜色

.net - 将 DataContext 绑定(bind)到 ValidationRule

c# - 将 JSON 对象反序列化为 .NET HashSet

c# - 使用 XAML 将 System.Drawing.Image 绑定(bind)到 System.Windows.Image 控件中

c# - 动态设置属性 WPF 应用程序