Skip to content

Bindables converts your auto properties into Wpf dependency or attached properties.

License

Notifications You must be signed in to change notification settings

jimmy-robert/Bindables

 
 

Repository files navigation

An add-in for Fody to convert regular properties into Dependency or Attached properties.

Bindables Icon

Bindables converts your auto properties into Wpf dependency or attached properties.
Additionally it allows you to set following options:

  • Specify a default value.
  • Specify FrameworkPropertyMetadataOptions.
  • Specify a PropertyChangedCallback method.
  • Register the property as readonly.

AppVeyor NuGet

How To Use

Dependency Property

You can convert regular properties to dependency properties by applying DependencyPropertyAttribute to individual properties or a whole class.

When you apply the attribute on a class, all the available properties will be converted to dependency properties. The properties that will not be converted are:

  • Non-auto properties. (Properties with custom getters and setters.)
  • Readonly properties. (Properties with no setter.)
  • Explicitly excluded properties. (Properties with [ExcludeDependencyProperty] attribute.)

When you apply the attribute on a class, you can further apply it to a property to specify options for that property.

Examples:

[DependencyProperty]
public class YourClass : DependencyObject
{
    public int RegularDependencyProperty { get; set; }

    // Dependency property with FrameworkPropertyMetadataOptions.
    [DependencyProperty(Options = FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)]
    public int WithOptions { get; set; }

    // Dependency property with a default value.
    public string Name { get; set; } = "Default";
    
    // Dependency property with PropertyChangedCallback.
    // This setting expects that a method with signature like below exists in the class.
    // static void NameOfTheMethod(DependencyObject, DependencyPropertyChangedEventArgs)
    [DependencyProperty(OnPropertyChanged = nameof(OnPropertyChanged))]
    public int WithPropertyChangedCallback { get; set; }
    
    private static void OnPropertyChanged(
        DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs eventArgs)
    {
    }
    
    // Dependency property with CoerceValueCallback.
    // This setting expects that a method with signature like below exists in the class.
    // static object NameOfTheMethod(DependencyObject, object)
    // You have to provide OnPropertyChanged along with OnCoerceValue. Otherwise a compiler error will be generated.
    [DependencyProperty(OnPropertyChanged = nameof(OnPropertyChanged), OnCoerceValue = nameof(OnCoerceValue))]
    public int WithCoerceValueCallback { get; set; }
    
    private static void OnCoerceValue(
        DependencyObject dependencyObject,
        object value)
    {
        return value;
    }
    
    // Readonly dependency property. The visibility modifier of the setter can be anything.
    [DependencyProperty(IsReadOnly = true)]
    public string ReadOnly { get; private set; }
}

Attached Property

The same principles with the dependency properties also apply to the attached property conversions. However, there are a few differences:

  • Use [AttachedProperty] instead of [DependencyProperty] and [ExcludeAttachedProperty] instead of [ExcludeDependencyProperty]
  • Properties must be static in order to be converted to attached properties.
  • The class containing the attached properties does not have to inherit from DependencyObject.
  • There should be a getter or setter method stub to make the xaml compiler happy. These methods can also be used explicitly when the attached value for an object has to be gotten or set in the code.
public class YourClass
{
    [AttachedProperty]
    public static string Name { get; set; }

    public static string GetName(DependencyObject obj)
    {
        // This method has to have the only line below.
        throw new WillBeImplementedByBindablesException();
    }

    public static void SetName(DependencyObject obj, string value)
    {
        // This method has to be empty.
    }
}

Icon

Link designed by Dmitry Mirolyubov from The Noun Project.

About

Bindables converts your auto properties into Wpf dependency or attached properties.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%