Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting attached property
Message
From
05/07/2011 11:30:49
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 4.0
Miscellaneous
Thread ID:
01516846
Message ID:
01517312
Views:
38
>>>Hi (long time no 'speak')
>>WPF Section here is pretty quiet. So I lurk.
>>
>>Your solution was exactly what I was hoping to see if someone could avoid <g>
>>
>>I'm really not a fan of massive switch statements when I can avoid them.
>>
>>Since no one came up with a wonderful solution using reflection... Here's what I was thinking:
>>
>>I have a quick and dirty utility I wrote that manages geometry in resource files and allows me to combine them into visuals. Similar to your scenario, it allows me to set properties on the objects. I haven't used it with attached properties, but in theory... that should work. What I do, is I read in the resource dictionary as XML. All the edits are done to the XML data. I display what I'm doing on screen by writing the XML to a memory stream and then I instantiate it using XamlReader.
>>
>>Here's a snippit:
>>
>>
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>
>
>Thanks for the suggestion. I think that approach would work but I finally got my way to work - the missing bit of the puzzle was *dynamic*. The DependencyData class is now:
[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() :-}

Nice!!!! (I need to play more with reflection and dynamic one of these days, I'm weak on them)

Glad you figured it out.
Previous
Reply
Map
View

Click here to load this message in the networking platform