复制(克隆)控件属性(使用反射)

        /// <summary>
        /// Clones the control.
        /// </summary>
        /// <param name="sourceElement">The source element.</param>
        /// <param name="destElement">The dest element.</param>
        public void CloneControl(UIElement sourceElement, UIElement destElement)
        {
            PropertyInfo[] controlProperties = destElement.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo propInfo in controlProperties)
            {
                if (propInfo.CanWrite && propInfo.CanRead)
                {
                    if (propInfo.Name == "Child")//这里可设置不克隆的属性名称
                        continue;
                    object value = propInfo.GetValue(sourceElement, null);
                    propInfo?.SetValue(destElement, value, null);
                }
            }
        }

 

posted @ 2016-12-20 11:48  WCode  阅读(531)  评论(0编辑  收藏  举报