I have a loop with children of a grid. For every child, I want to know which properties have been specified explicitly in the XAML code. Do I have a chance to find out?
For example: I have a TextBox
<TextBox Height="150"/>
Only the property Height is given by XAML-Code. How I can find it out in the c# Code? In other words, I don't want all of the properties of the TextBox, but only those who are specified in the XAML.
This was a challenging question, but luckily for you, I like a good challenge. So initially, I found the DependencyPropertyHelper.GetValueSource method. This method takes a DependencyObject and a DependencyProperty and returns a ValueSource struct:
ValueSource valueSource = DependencyPropertyHelper.GetValueSource(SomeTextBlock,
TextBlock.TextWrappingProperty);
The ValueSource struct has a BaseValueSource enum property which has the following members:

These values relate to the list of DependencyProperty precedences and specify the different ways that a DependencyProperty value can be changed. A BaseValueSource enum instance value of Local means that the property was set locally using the SetValue method... this will also include instances where a property was set in code using the SetValue method. The Framework uses this method to set values from XAML markup.
The only problem was that we'd now have to find a collection of all of the DependencyPropertys of a particular DependencyObject so that we could call the above method on each to see if it was set by the SetValue method. I was hoping that Microsoft would have given us something to do this for us, but it appears not.
After a quick search, I found a way to do this using Reflection in the List properties of a DependencyObject? post here on StackOverflow. However, I kept looking and then came across a better method... a much better method. I found it in the Getting list of all dependency/attached properties of an Object question on the Visual Studio Forum.
If you look down that page at the answer from Zhou Yong, you can find a DependencyPropertyHelper class that he created. At first I thought 'let me just run this DependencyPropertyHelper.GetValueSource method to see what I get' and was expecting a long list of all of the DependencyPropertys of the TextBlock.
However, it turns out that what comes out of this method is exactly what you're after. It only returns the properties that have actually been set in XAML. In his code, I see a MarkupObject, a MarkupProperty and a MarkupWriter. I've not used these before, but it seems that this is actually looking at the XAML defined for the TextBlock. So in helping you, I've actually learned some new things too... +1 good question.
Bearing that in mind, I believe that you can ignore the earlier part of my answer regarding the ValueSource struct and just use that method. Let me know If you need any more help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With