How to determine whether a dependency object implements a given dependency property (C# / WPF)
Posted
by Tim Coulter
on Stack Overflow
See other posts from Stack Overflow
or by Tim Coulter
Published on 2010-04-22T13:23:19Z
Indexed on
2010/04/23
9:53 UTC
Read the original article
Hit count: 465
dependency-properties
|dependency-object
I am working with the classes in the System.Windows.Documents
namespace, trying to write some generic code that will conditionally set the value of certain dependency properties, depending on whether these properties exist on a given class.
For example, the following method assigns an arbitrary value to the Padding
property of the passed FrameworkContentElement
:
void SetElementPadding(FrameworkContentElement element)
{
element.SetValue(Block.PaddingProperty, new Thickness(155d));
}
However, not all concrete implementations of FrameworkContentElement
have a Padding
property (Paragraph
does but Span
does not) so I would expect the property assignment to succeed for types that implement this property and to be silently ignored for types that do not.
But it seems that the above property assignment succeeds for instances of all derivatives of FrameworkContentElement
, regardless of whether they implement the Padding
property. I make this assumption because I have always been able to read back the assigned value.
I assume there is some flaw in the way I am assigning property values. What should I do to ensure that a given dependency property assignment is ignored by classes that do not implement that property?
Many thanks for your advice.
Tim
© Stack Overflow or respective owner