问题
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