Is there a way to automatically create DependencyProperty in MVVM
Asked Answered
G

3

5

More of a usability question. I'm building a big MVVM application, and I find myself copying / modifying this piece of code all over the place:

public NodeKind Kind
{
    get { return (NodeKind)this.GetValue(KindProperty); }
    set { this.SetValue(KindProperty, value); }
}

public static readonly DependencyProperty KindProperty = DependencyProperty.Register(
        "Kind", typeof(NodeKind), typeof(DashboardNode));

Is there a way in Visual Studio to generate this code with a shortcut or something? I have Resharper and VS 2015, and I can't find the command that would automatically do that for me.

Garderobe answered 9/3, 2017 at 9:24 Comment(0)
C
12

If you use ReSharper the dependencyProperty snippet is generating this. Without ReSharper the propdp snippet is creating the same.

Chasten answered 9/3, 2017 at 9:26 Comment(0)
J
6

There's the propdp snippet. Just type propdp and press TAB twice. A similar snippet exists for attached properties as well: propa

Jennefer answered 9/3, 2017 at 9:26 Comment(0)
C
5

I created some snippets to create read/write and readonly dependency properties that include change event handlers. If change event handlers aren't desired, you just have to drag-select from the closing bracket of the event handler to the comma before the event handler is passed into the property metadata and type ));.

Here's the read/write version

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>DependencyProperty</Title>
      <Author>will</Author>
      <Description>DependencyProperty
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>dp</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>PropName</ID>
          <ToolTip>Property name</ToolTip>
          <Default>PropertyName</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="false">
          <ID>ClassName</ID>
          <ToolTip>Class name</ToolTip>
          <Default>ClassName</Default>
          <Function>ClassName()</Function>
        </Literal>
        <Literal Editable="true">
          <ID>Type</ID>
          <ToolTip>Property type</ToolTip>
          <Default>object</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>DefaultValue</ID>
          <ToolTip>Default value</ToolTip>
          <Default>null</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[#region $PropName$
/// <summary>
/// The <see cref="DependencyProperty"/> for <see cref="$PropName$"/>.
/// </summary>
public static readonly DependencyProperty $PropName$Property =
    DependencyProperty.Register(
        $PropName$PropertyName, 
        typeof($Type$), 
        typeof($ClassName$), 
        new UIPropertyMetadata($DefaultValue$, On$PropName$PropertyChanged));

/// <summary>
/// Called when the value of <see cref="$PropName$Property"/> changes on a given instance of <see cref="$ClassName$"/>.
/// </summary>
/// <param name="d">The instance on which the property changed.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void On$PropName$PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    (d as $ClassName$).On$PropName$Changed(e.OldValue as $Type$, e.NewValue as $Type$);
}

/// <summary>
/// Called when <see cref="$PropName$"/> changes.
/// </summary>
/// <param name="oldValue">The old value</param>
/// <param name="newValue">The new value</param>
private void On$PropName$Changed($Type$ oldValue, $Type$ newValue)
{
    ;
}

/// <summary>
/// The name of the <see cref="$PropName$"/> <see cref="DependencyProperty"/>.
/// </summary>
public const string $PropName$PropertyName = "$PropName$";

/// <summary>
/// $end$
/// </summary>
public $Type$ $PropName$
{
    get { return ($Type$)GetValue($PropName$Property); }
    set { SetValue($PropName$Property, value); }
}
#endregion  ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

And the read-only version

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <AlternativeShortcuts>
        <Shortcut Value="rodp">Read Only Dependency Property</Shortcut>
      </AlternativeShortcuts>
      <Title>Readonly DependencyProperty</Title>
      <Author>Will Sullivan</Author>
      <Description>Readonly DependencyProperty</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>RODP</Shortcut>
    </Header>
    <Snippet>
      <References>
      </References>
      <Imports>
        <Import>
          <Namespace>System.Windows</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal Editable="false">
          <ID>ClassName</ID>
          <ToolTip>The class name</ToolTip>
          <Default>ClassName</Default>
          <Function>ClassName()</Function>
        </Literal>
        <Literal Editable="true">
          <ID>Type</ID>
          <ToolTip>Property type</ToolTip>
          <Default>object</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>PropName</ID>
          <ToolTip>Property name</ToolTip>
          <Default>PropertyName</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[#region $PropName$
/// <summary>
/// The <see cref="DependencyPropertyKey"/> for $PropName$.
/// </summary>
private static readonly DependencyPropertyKey $PropName$Key 
    = DependencyProperty.RegisterReadOnly(
        $PropName$PropertyName, 
        typeof($Type$), 
        typeof($ClassName$), 
        new PropertyMetadata());

/// <summary>
/// The <see cref="DependencyProperty"/> for $PropName$.
/// </summary>
public static readonly DependencyProperty $PropName$Property 
    = $PropName$Key.DependencyProperty;

/// <summary>
/// The name of the <see cref="$PropName$"/> <see cref="DependencyProperty"/>.
/// </summary>
public const string $PropName$PropertyName = "$PropName$";

/// <summary>
/// $end$
/// </summary>
public $Type$ $PropName$
{
    get { return ($Type$)GetValue($PropName$Property ); }
    private set { SetValue($PropName$Key, value); }
}
#endregion

]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Councilwoman answered 9/3, 2017 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.