123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- namespace FWindSoft.Wpf.Controls
- {
- /*
- 模板相关类集成
- */
- /// <summary>
- /// 数据模板解析类
- /// </summary>
- public interface ITemplateParse
- {
- DataTemplate SelectTemplate(object ob);
- }
- /// <summary>
- /// 数据模板解析特性
- /// </summary>
- public class TemplateParseAttribute : Attribute
- {
- private Type m_Type;
- public TemplateParseAttribute(Type parseType)
- {
- if (parseType.IsAssignableFrom(typeof(ITemplateParse)))
- throw new ArgumentException("类型必须继承自ITemplateParse");
- m_Type = parseType;
- }
- /// <summary>
- /// 创建机械实例
- /// </summary>
- /// <returns></returns>
- public ITemplateParse CreateInstance()
- {
- if (m_Type == null)
- return null;
- return Activator.CreateInstance(m_Type, true) as ITemplateParse;
- }
- }
- /// <summary>
- /// 数据模板解析管理
- /// </summary>
- public class CommonTemplateProvider
- {
- public static readonly CommonTemplateProvider Default = new CommonTemplateProvider();
- public readonly Dictionary<Type, ITemplateParse> m_DicParse = new Dictionary<Type, ITemplateParse>();
- public DataTemplate DefaultTemplate { get; set; }
- /// <summary>
- /// 增加映射函数
- /// </summary>
- /// <param name="type"></param>
- /// <param name="parse"></param>
- public void Add(Type type, ITemplateParse parse)
- {
- m_DicParse[type] = parse;
- }
- /// <summary>
- /// 根据对象获取模板
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- public virtual DataTemplate GetTemplate(object item)
- {
- ITemplateParse parse = null;
- DataTemplate resultTemplate = null;
- do
- {
- if (item == null)
- {
- break;
- }
- Type type = item as Type;
- if (type == null)
- {
- type = item.GetType();
- }
- if (m_DicParse.TryGetValue(type, out parse))
- {
- break;
- }
- var attributes = type.GetCustomAttributes(typeof(TemplateParseAttribute), true);
- if (attributes.Any())
- {
- parse = (attributes[0] as TemplateParseAttribute).CreateInstance();
- break;
- }
- } while (false);
- if (parse != null)
- {
- resultTemplate = parse.SelectTemplate(item);
- }
- return resultTemplate ?? GetDefaultTemplate(item);//如果为空,返回一个默认的模板,textblok
- }
- /// <summary>
- /// 根据Id获取模板
- /// </summary>
- /// <param name="templateId"></param>
- /// <returns></returns>
- public virtual DataTemplate GetTemplateByKey(string templateId)
- {
- //暂时先这么粗糙的写
- return GetTemplate(templateId);
- }
- /// <summary>
- /// 获取默认的模板
- /// </summary>
- /// <returns></returns>
- protected virtual DataTemplate GetDefaultTemplate(object item)
- {
- if (DefaultTemplate != null)
- return DefaultTemplate;
- DataTemplate temp = new DataTemplate();
- FrameworkElementFactory root = new FrameworkElementFactory(typeof(TextBox),"Part_Text");
- Binding binding = new Binding("Value");
- binding.Mode = BindingMode.TwoWay;
- root.SetBinding(TextBox.TextProperty, binding);
- //root.SetValue(TextBox.ForegroundProperty, Brushes.Red);
- root.SetValue(ElementCopyOptions.UseCopyProperty, true);
- root.SetBinding(ElementCopyOptions.CopyValueProperty, binding);
- temp.VisualTree = root;
- temp.Seal();
- return temp;
- }
- }
- }
|