復制(克隆)控件屬性(使用反射),克隆控件
/// <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);
}
}
}