Define a field with type Dependency Property Type below and give it a name that ends with Property Suffix. Then, add Attribute Type attribute to the field and pass the type of the property you want to create. Possible values are:
Project Type | Dependency Property Type | Access Type | Property Suffix | Field Type | Attribute Type |
---|---|---|---|---|---|
WPF | Dependency Property | Read/Write | Property |
DependencyProperty |
Bindables.Wpf.DependencyPropertyAttribute |
WPF | Dependency Property | Read Only | PropertyKey |
DependencyPropertyKey |
Bindables.Wpf.DependencyPropertyAttribute |
WPF | Attached Property | Read/Write | Property |
DependencyProperty |
Bindables.Wpf.AttachedPropertyAttribute |
WPF | Attached Property | Read Only | PropertyKey |
DependencyPropertyKey |
Bindables.Wpf.AttachedPropertyAttribute |
Xamarin.Forms | Bindable Property | Read/Write | Property |
BindableProperty |
Bindables.Forms.BindablePropertyAttribute |
Xamarin.Forms | Bindable Property | Read Only | PropertyKey |
BindablePropertyKey |
Bindables.Forms.BindablePropertyAttribute |
Xamarin.Forms | Attached Property | Read/Write | Property |
BindableProperty |
Bindables.Forms.AttachedPropertyAttribute |
Xamarin.Forms | Attached Property | Read Only | PropertyKey |
BindablePropertyKey |
Bindables.Forms.AttachedPropertyAttribute |
You can pass following options via the attributes:
Option | Description |
---|---|
OnPropertyChanged |
Name of the method that will be called when the property is changed. |
DefaultValueField |
Name of the static field that will provide the default value for the property. |
Options (WPF only) |
Pass System.Windows.FrameworkPropertyMetadataOptions to the dependency property. |
BindingMode (Xamarin.Forms only) |
Pass Xamarin.Forms.BindingMode to the dependency property. |
Signature of OnPropertyChanged
method should be:
Project Type | Signature |
---|---|
WPF | static void MethodName(DependencyObject obj, DependencyPropertyChangedEventArgs args) |
Xamarin.Forms | static void MethodName(BindableObject obj, object oldValue, object newValue) |
- Your class should be
partial
. - If you create a WPF dependency property or Xamarin.Forms bindable property, your class should inherit from
System.Windows.DependencyObject
orXamarin.Forms.BindableObject
. Attached properties don't have this requirement. - Bindables creates the static constructor for the class to initialize the dependency properties. If you have custom static constructor for a type, you can't use Bindables on it.
using System.Windows;
using Bindables.Wpf;
public partial class YourClass : DependencyObject
{
private static readonly string DefaultValue = "Test";
[DependencyProperty(typeof(string))]
public static readonly DependencyProperty RegularProperty;
// You can use any visibility modifier.
[DependencyProperty(typeof(string))]
private static readonly DependencyPropertyKey ReadOnlyPropertyKey;
[DependencyProperty(typeof(string), OnPropertyChanged = nameof(PropertyChangedCallback), DefaultValueField = nameof(DefaultValue), Options = FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)]
public static readonly DependencyProperty CustomizedProperty;
private static void PropertyChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
}
}
// Generated by Bindables
using System.Windows;
public partial class YourClass
{
public string Regular
{
get => (string)GetValue(RegularProperty);
set => SetValue(RegularProperty, value);
}
public static readonly DependencyProperty ReadOnlyProperty;
public string ReadOnly
{
get => (string)GetValue(ReadOnlyProperty);
private set => SetValue(ReadOnlyPropertyKey, value);
}
public string Customized
{
get => (string)GetValue(CustomizedProperty);
set => SetValue(CustomizedProperty, value);
}
static YourClass()
{
RegularProperty = DependencyProperty.Register(
nameof(Regular),
typeof(string),
typeof(YourClass),
new PropertyMetadata());
ReadOnlyPropertyKey = DependencyProperty.RegisterReadOnly(
nameof(ReadOnly),
typeof(string),
typeof(YourClass),
new PropertyMetadata());
ReadOnlyProperty = ReadOnlyPropertyKey.DependencyProperty;
CustomizedProperty = DependencyProperty.Register(
nameof(Customized),
typeof(string),
typeof(YourClass),
new FrameworkPropertyMetadata(DefaultValue, (FrameworkPropertyMetadataOptions)256, PropertyChangedCallback));
}
}