/// <summary>
        /// Creates a new instance <see cref="DataSourceDefinition"/> for the specified component template.
        /// </summary>
        /// <param name="templatedControl">The type of control to which the template is applied.</param>
        /// <param name="definition">The XML element that defines the component template.</param>
        /// <returns>The <see cref="DataSourceDefinition"/> that was created.</returns>
        public static DataSourceDefinition FromComponentTemplate(Type templatedControl, XElement definition)
        {
            var dataSourceIdentifier       = templatedControl.Name;
            var dataSourceWrapperName      = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templatedControl);
            var dataSourceWrapperNamespace = PresentationFoundationView.DataSourceWrapperNamespaceForComponentTemplates;

            return(new DataSourceDefinition(templatedControl.FullName,
                                            dataSourceIdentifier, dataSourceWrapperName, dataSourceWrapperNamespace, templatedControl, definition));
        }
        /// <summary>
        /// Finds all of the compiled binding expressions on the current data
        /// source and adds them to the context's registry.
        /// </summary>
        private void FindCompiledBindingExpressions()
        {
            var wrapperName = default(String);
            var wrapperType = DataSource is PresentationFoundationView ? DataSourceType : null;

            if (wrapperType == null)
            {
                for (var templateType = TemplatedParent.GetType(); templateType != null; templateType = templateType.BaseType)
                {
                    wrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templateType);
                    wrapperType = Ultraviolet.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(wrapperName);

                    if (wrapperType != null)
                    {
                        break;
                    }
                }

                if (wrapperType == null)
                {
                    wrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(TemplatedParent.GetType());
                    throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(wrapperName));
                }
            }

            var properties = wrapperType.GetProperties().Where(x => x.Name.StartsWith("__UPF_Expression")).ToList();
            var propertiesWithExpressions = from prop in properties
                                            let attr                       = (CompiledBindingExpressionAttribute)prop.GetCustomAttributes(typeof(CompiledBindingExpressionAttribute), false).Single()
                                                                  let expr = attr.Expression
                                                                             select new
            {
                Property   = prop,
                Expression = expr,
            };

            foreach (var prop in propertiesWithExpressions)
            {
                var key = new CompiledBindingExpressionKey(prop.Property.PropertyType, prop.Expression);
                compiledBindingExpressions.Add(key, prop.Property);
            }

            var uniques = compiledBindingExpressions.GroupBy(x => x.Key).Where(x => x.Count() == 1).ToList();

            foreach (var unique in uniques)
            {
                var key = new CompiledBindingExpressionKey(null, unique.Key.Expression);
                compiledBindingExpressions[key] = unique.Single().Value;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Performs initialization required by instances of the <see cref="ContentPresenter"/> class.
        /// </summary>
        private void InitializeContentPresenter(UltravioletContext uv, Object instance, UvmlInstantiationContext context)
        {
            var contentPresenter = instance as ContentPresenter;

            if (contentPresenter == null)
            {
                return;
            }

            if (contentPresenter.HasDefinedValue(ContentPresenter.ContentProperty) || contentPresenter.TemplatedParent == null)
            {
                return;
            }

            var alias = contentPresenter.ContentSource ?? "Content";

            if (alias == String.Empty)
            {
                return;
            }

            var templateType        = contentPresenter.TemplatedParent.GetType();
            var templateWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templateType);
            var templateWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(templateWrapperName);

            var dpAliasedContent = DependencyProperty.FindByName(alias, templateType);

            if (dpAliasedContent != null)
            {
                contentPresenter.BindValue(ContentPresenter.ContentProperty, templateWrapperType,
                                           "{{" + dpAliasedContent.Name + "}}");
            }

            if (!contentPresenter.HasDefinedValue(ContentPresenter.ContentStringFormatProperty))
            {
                var dpAliasedContentStringFormat = DependencyProperty.FindByName(alias + "StringFormat", templateType);
                if (dpAliasedContentStringFormat != null)
                {
                    contentPresenter.BindValue(ContentPresenter.ContentStringFormatProperty, templateWrapperType,
                                               "{{" + dpAliasedContentStringFormat.Name + "}}");
                }
            }
        }