When looking at sample attached properties and behaviors, I've seen a mishmash of uses of FrameworkPropertyMetadata
, UIPropertyMetadata
and PropertyMetadata
. Since they all form an inheritance hierarchy, how do I choose which one to use?

- 89,048
- 55
- 235
- 380
2 Answers
These classes are to report some behavior aspects of a dependency property.
Check the different classes for the options they provide.
For example,
if you just want to back a property by dp and provide a default value, use PropertyMetadata
,
if you want to specify animation behavior, use UIPropertyMetadata
,
but if some property affects wpf framework level stuffs eg element layout, parent layout or databinding, use FrameworkPropertyMetadata
.
Details you can check on msdn http://msdn.microsoft.com/en-us/library/ms751554.aspx
-
2The real question is, why does the propdp snippet use UIPropertyMetadata, especially since there are no PropertyMetadata subclasses in Silverlight? It drives me nuts. – Grank Jun 29 '10 at 23:32
-
6That's because same snippets are shared between WPF and Silverlight. You can try different snippets for silverlight from this link: http://blog.nerdplusart.com/archives/silverlight-code-snippets – gp. Jul 08 '10 at 07:54
-
4MSDN says "In general, you should use FrameworkPropertyMetadata, particularly if your property has any interaction with property system and WPF functions such as layout and data binding." - (Specifying Metadata) http://msdn.microsoft.com/en-us/library/ms751554.aspx – Wayne Bloss Oct 28 '11 at 17:02
-
What kind of data binding should FrameworkPropertyMetadata be used for? I simply want a property that is bound to IsEnabled of a control. – Monstieur Jun 04 '13 at 07:02
One useful feature of FrameworkPropertyMetadata
is that you can define the behaviour BindsTwoWayByDefault
. Otherwise the dependency property is OneWay by default.
When you need a two way binding for a dependency property, then normally you always need to define Mode=TwoWay
for each binding. If you set this mode as default, you don't need to set it for each binding anymore.
You set the behaviour like this:
new FrameworkPropertyMetadata(_myDefaultValue_, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
Complete example:
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
"IsSelected", typeof(bool), typeof(MyClass),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}

- 865
- 11
- 14