Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting attached property
Message
From
04/07/2011 06:03:56
 
 
To
03/07/2011 21:05:04
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 4.0
Miscellaneous
Thread ID:
01516846
Message ID:
01517242
Views:
34
>>Hi,
>>How to implement this:
private void SetAttachedProperty(FrameworkElement fe, string propertyName, object value)
>>        {
>>        }
when called, for example, with:
SetAttachedProperty(new TextBlock(), "Canvas.Left", 50);
>>>>>>>
>
>I was watching this one to see if any one had an answer. So far no joy :-(
>
>Have you considered using XamlReader/Writer for this instead of rolling your own property setter?

Hi (long time no 'speak')
I don't think I can use that (but I might be missing something). Background:

Say I have something like a TexBlock contained in a Canvas with various properties of the TextBlock set in XAML. At runtime the use can change the TextBlock properties by setting properties directly or by dragging/resizing etc.
I need to track exactly only those properties have been changed and, in a future instantiation, apply those changes to the original TextBlock. I have code that captures the value of each property in a list when the layout is initially loaded. I get another list when the user wishes to save the changes and then compare the two lists to end up with a final List of just those properties which have changed. The list is a collection of:
[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 :-{
Any suggestions,
Regards,
Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform