Ejemplo n.º 1
0
        /// <summary>
        /// Materializes a piece of data based on a data template.
        /// </summary>
        /// <param name="control">The control materializing the data template.</param>
        /// <param name="data">The data.</param>
        /// <returns>The data materialized as a control.</returns>
        public static IControl MaterializeDataTemplate(this IControl control, object data)
        {
            if (data == null)
            {
                return(null);
            }
            else
            {
                var asControl = data as IControl;

                if (asControl != null)
                {
                    return(asControl);
                }
                else
                {
                    IDataTemplate template = control.FindDataTemplate(data);
                    IControl      result;

                    if (template != null)
                    {
                        result = template.Build(data);
                    }
                    else
                    {
                        result = FuncDataTemplate.Default.Build(data);
                    }

                    NameScope.SetNameScope((Control)result, new NameScope());

                    return(result);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Materializes a piece of data based on a data template.
        /// </summary>
        /// <param name="control">The control materializing the data template.</param>
        /// <param name="data">The data.</param>
        /// <returns>The data materialized as a control.</returns>
        public static IControl MaterializeDataTemplate(this IControl control, object data)
        {
            IDataTemplate template = control.FindDataTemplate(data);
            IControl      result;

            if (template != null)
            {
                result = template.Build(data);

                if (result != null && result.DataContext == null)
                {
                    result.DataContext = data;
                }
            }
            else if (data is IControl)
            {
                result = (IControl)data;
            }
            else
            {
                result = DataTemplate.Default.Build(data);
            }

            return(result);
        }