Skip to content

Latest commit

 

History

History
94 lines (71 loc) · 2.72 KB

File metadata and controls

94 lines (71 loc) · 2.72 KB

NuGet Status

Icon

Implement automatically DependencyPropety in WPF.

Usage

See also Fody usage.

NuGet installation

Install the Kasay.DependencyProperty.WPF.Fody NuGet package:

PM> Install-Package Kasay.DependencyProperty.WPF.Fody -Version 1.0.3	

Add to FodyWeavers.xml

** it's generated automatically after build.

Add <Kasay.DependencyProperty.WPF/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <Kasay.DependencyProperty.WPF />
</Weavers>

Overview

Before code:

public class DemoControl : UserControl
{
  [Bind] public String SomeName { get; set; }

  [Bind] public Int32 SomeNumber { get; set; }

  [Bind] public Boolean SomeCondition { get; set; }
}

What gets compiled:

public class DemoControl : UserControl
{
    public static readonly DependencyProperty SomeNameProperty 
        = DependencyProperty.Register(
            "SomeName", 
            typeof(String), 
            typeof(DemoControl));

    public String SomeName
    {
        get => (String)GetValue(SomeNameProperty);
        set => SetValue(SomeNameProperty, value);
    }

    public static readonly DependencyProperty SomeNumberProperty 
        = DependencyProperty.Register(
            "SomeNumber", 
            typeof(Int32), 
            typeof(DemoControl));

    public Int32 SomeNumber
    {
        get => (Int32)GetValue(SomeNumberProperty);
        set => SetValue(SomeNumberProperty, value);
    }

    public static readonly DependencyProperty SomeConditionProperty 
        = DependencyProperty.Register(
            "SomeCondition", 
            typeof(Boolean),
            typeof(DemoControl));

    public Boolean SomeCondition
    {
        get => (Boolean)GetValue(SomeConditionProperty);
        set => SetValue(SomeConditionProperty, value);
    }

    public DemoControl()
    {
        DataContext = this;
    }
}

As Observed DependencyProperty declarations in WPF are redundant and repetitive, but adding the attibute Bind to each property leaves the code clean.