I was under the impression that CLR wrappers for dependency properties were optional under WPF, and just useful for setting within your own code.
However, I have created a UserControl without wrappers, but some XAML that uses it will not compile without them:
namespace MyControlLib
{
public partial class MyControl : UserControl
{
public static readonly DependencyProperty SomethingProperty;
static MyControl()
{
SomethingProperty = DependencyProperty.Register("Something", typeof(int), typeof(MyControl));
}
}
}
XAML usage:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:MyControlLib;assembly=MyControlLib">
<ctrl:MyControl Something="45" />
</Window>
Trying to compile this gives:
error MC3072: The property ‘Something’ does not exist in XML namespace ‘clr-namespace:MyControlLib’. Line blah Position blah.
Adding a CLR wrapper in MyControl.xaml.cs like:
public int Something
{
get { return (int)GetValue(SomethingProperty); }
set { SetValue(SomethingProperty, value); }
}
means it all compiles and works fine.
What am I missing?
You could use dependency properties without wrappers inside runtime bindings, but to set the property like you want you must have C# property to allow xaml compiler to compile your code.