private void SetAttachedProperty(FrameworkElement fe, string propertyName, object value) >>> { >>> }when called, for example, with:
SetAttachedProperty(new TextBlock(), "Canvas.Left", 50);>>>>>>>>
[Serializable] > public class DependencyData > { > public DependencyData(string s, object o, Type t) > { > FieldName = s; FieldValue = o; OwnerType = t; > } > public string FieldName { get; set; } > public object FieldValue { get; set; } > //Not neccessary - added for debugging > public Type OwnerType { get; set; } > }For normal dependency properties I can just use the 'else' approach:
Type atType = AT.GetType(); // AT is, say, the TextBlock > foreach (DependencyData d in changedProperties) > { > PropertyInfo pi = atType.GetProperty(d.FieldName); > if (pi == null) > { > //Problem here if pi is an attached property > } > else > pi.SetValue(AT, d.FieldValue, null); > } > }At the moment in the (pi==null) I've resorted to the clunky:
string theClass = d.FieldName.Substring(0, d.FieldName.IndexOf('.')); > string theProperty = d.FieldName.Substring(d.FieldName.IndexOf('.') + 1); > switch (theClass) > { > case "Canvas": > { > switch (theProperty) > { > case "Left": > { > Canvas.SetLeft(AT, (double)d.FieldValue); > break; > } > //etc > default: > { > throw new Exception(theProperty + " is unhandled Canvas"); > } > } > break; > } > //etc > default: > { > throw new Exception(theClass + "not handled for DependencyProperties"); > } > }but I really need a more generic, less exception prone approach :-{
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);Don't know if that idea will work for you, but I figured it was better than no idea. <g>