using System; using System.Globalization; using System.Windows.Data; using System.Windows.Markup; namespace WPF.Common { /// /// Base class for value converters. /// This class provides the following features: /// - Allow the derived value converter to be used as a markup extension, /// thus making the XAML code much more simpler: /// Instead of: /// Container.Resources /// src:MyConverter x:Key="MyConverter" /// Container.Resources /// /// Converter={StaticResource MyConverter} /// /// Use: Converter={src:MyConverter} /// /// - The derived converter has only one instance (singleton). /// - Default implementation for unsupported value conversions (throws NotImplementedException); /// /// Your value converter public abstract class ConverterMarkupExtension : MarkupExtension, IValueConverter, IMultiValueConverter where T : class, new() { /// /// Holds reference to the converter single instance /// private static T _converter = null; /// /// Returns an object that is set as the value of the target property for this markup extension. /// /// Object that can provide services for the markup extension. /// The converter to set on the property where the extension is applied. public override object ProvideValue(IServiceProvider serviceProvider) { if (_converter == null) { _converter = new T(); } return _converter; } #region IValueConverter Members /// /// Converts a value. /// Default implementation is to throw NotImplementedException. /// /// The value produced by the binding source. /// The type of the binding target property. /// The converter parameter to use. /// The culture to use in the converter. /// A converted value. If the method returns null, the valid null value is used. public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } /// /// Converts a value back. /// Default implementation is to throw NotImplementedException. /// /// The value that is produced by the binding target. /// The type to convert to. /// The converter parameter to use. /// The culture to use in the converter. /// A converted value. If the method returns null, the valid null value is used. public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion #region IMultiValueConverter Members /// /// Converts source values to a value for the binding target. The data binding /// engine calls this method when it propagates the values from source bindings /// to the binding target. /// Default implementation is to throw NotImplementedException. /// /// The array of values that the source bindings in the System.Windows.Data.MultiBinding /// produces. The value System.Windows.DependencyProperty.UnsetValue indicates /// that the source binding has no value to provide for conversion. /// The type of the binding target property. /// The converter parameter to use. /// The culture to use in the converter. /// A converted value.If the method returns null, the valid null value is used. /// A return value of System.Windows.DependencyProperty.System.Windows.DependencyProperty.UnsetValue /// indicates that the converter did not produce a value, and that the binding /// will use the System.Windows.Data.BindingBase.FallbackValue if it is available, /// or else will use the default value.A return value of System.Windows.Data.Binding.System.Windows.Data.Binding.DoNothing /// indicates that the binding does not transfer the value or use the System.Windows.Data.BindingBase.FallbackValue /// or the default value. public virtual object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } /// /// Converts a binding target value to the source binding values. /// Default implementation is to throw NotImplementedException. /// /// The value that the binding target produces. /// The array of types to convert to. The array length indicates the number and /// types of values that are suggested for the method to return. /// The converter parameter to use. /// The culture to use in the converter. /// An array of values that have been converted from the target value back to /// the source values. public virtual object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion } }