XmlDocument doc = new XmlDocument(); > XmlNode node = doc.ImportNode(_Node.Clone(), true); > node.Attributes.RemoveNamedItem("x:Key"); > doc.AppendChild(node); > MemoryStream strm = new MemoryStream(); > doc.Save(strm); > //FileStream fs = File.Create("test.xaml"); // For Testing > //fs.Write(strm.ToArray(),0,(int)strm.Length); > //fs.Close(); > strm.Position = 0; > _Object = XamlReader.Load(strm);>
[Serializable] > public class DependencyData > { > public DependencyData(string s, object o, String t, Type ot) > { > FieldName = s; FieldValue = o; DependencyPropertyName = t; OwnerType = ot; > } > public string FieldName { get; set; } > public object FieldValue { get; set; } > public string DependencyPropertyName { get; set; } > public Type OwnerType { get; set; } > }Populated by:
MarkupObject muo = MarkupWriter.GetMarkupObjectFor(fe); > List<DependencyData> list = (from x in muo.Properties > where x.IsAttached select > new DependencyData(x.Name, x.Value, x.DependencyProperty.Name, x.DependencyProperty.OwnerType)).ToList();Regular DependencyProperties are also added to the list.Then this will reinstate the values:
Type atType = AT.GetType(); > foreach (DependencyData d in changedProperties) > { > PropertyInfo pi = atType.GetProperty(d.FieldName); > if (pi == null) > { > //Attached properties > dynamic c = Activator.CreateInstance(d.OwnerType); > FieldInfo fi = c.GetType().GetField(d.DependencyPropertyName + "Property"); > DependencyProperty dp = (DependencyProperty)fi.GetValue(c); > AT.SetValue(dp, d.FieldValue); > } > else > //Regular DependencyProperties > pi.SetValue(AT, d.FieldValue, null); > }>I think I'd have shot myself if I'd had to stick with the switch() :-}