public bool CanProvideConverter(BinderViewBase view, BinderHint hint)
 {
     return hint == BinderHint.Money || hint == BinderHint.Weight ||
         hint == BinderHint.Length || hint == BinderHint.NaturalNumber ||
         hint == BinderHint.Year || hint == BinderHint.Percentage ||
         hint == BinderHint.MinutesDuration || hint == BinderHint.HoursAndMinutesDuration ||
         hint == BinderHint.MoneyString;
 }
        public ViewConverterBase ProvideConverter(BinderViewBase view, BinderHint hint)
        {
            if (hint == BinderHint.Money)
                return new ConvertConverter(typeof(decimal));

            if (hint == BinderHint.Weight || hint == BinderHint.Length ||
                hint == BinderHint.Percentage || hint == BinderHint.MinutesDuration ||
                hint == BinderHint.HoursAndMinutesDuration)
                return new ConvertConverter(typeof(double));

            if (hint == BinderHint.NaturalNumber || hint == BinderHint.Year)
                return new ConvertConverter(typeof(int));

            if (hint == BinderHint.MoneyString)
                return new FormatConverter("${0:0.00}");

            throw new InvalidOperationException();
        }
示例#3
0
        public BinderViewBase AutoBind(object viewer, BinderHint hint)
        {
            foreach (IAutoBindingFactory factory in _autoBindings)
            {
                if (factory.CanAutoBind(viewer))
                {
                    BinderViewBase view = factory.CreateView(viewer);

                    // Find a converter:
                    ViewConverterBase converter = ViewConverterBase.Null;

                    foreach (IAutoConverterFactory converterFactory in _autoConverters)
                    {
                        if (converterFactory.CanProvideConverter(view, hint))
                        {
                            converter = converterFactory.ProvideConverter(view, hint);

                            break;
                        }

                        // (If a converter can't be provided, just use the exact one.)
                    }

                    AddBinding(FixAutoBindName(factory.GetAutoBindName(viewer)), view, converter);

                    return view;
                }
            }

            throw new InvalidOperationException("No suitable autobinder was found.");
        }