次の方法で共有


Using VS Snippets to Add DependencyProperties and Attached DependencyProperties

It can be a little tedious to type in everything that you need to define a DependencyProperty but you can use VS 2008's snippets to make is easy. The snippets are designed to work with WPF, but they can be made to work with Silverlight with one small change.

First, you need to insert the snippet. In the VS 2008 editor, right-click at the location in your class file where you want to define the DependencyProperty, and select "Insert Snippet" from the context menu.

When I do that, I get another menu with "NetFX30", "Other", and "C#" options. Select "NetFX30".

Now, you'll get another menu with "Define a DependencyProperty" and "Define an attached DependencyProperty". Choose whichever one you like.

You'll see something like the code below. You can tab between the highlighted text (the border indicates which one you are updating), and enter your own values.

public int MyProperty

{

get { return (int)GetValue(MyPropertyProperty); }

set { SetValue(MyPropertyProperty, value); }

}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...

public static readonly DependencyProperty MyPropertyProperty =

DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));

When you change a value, VS will update the rest of the snippet. Once you have entered the property type, name, and ownerclass, you need to decide what to do with the UIPropertyMetadata. Silverlight does not support that class, so if you want to add a PropertyChangedCallback, you'll have to change the type from UIPropertyMetadata to PropertyMetadata, and pass the delegate to the ctor. This looks like this:

// Using a DependencyProperty as the backing store for Foo. This enables animation, styling, binding, etc...

public static readonly DependencyProperty FooProperty =

    DependencyProperty.Register("Foo", typeof(double), typeof(CustomAttachedDP), new PropertyMetadata(OnFooChanged));

private void OnFooChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

{

}

 

If you don't want a PropertyChangedCallback, just pass null instead of the PropertyMetadata.

It is pretty easy to modify snippets and create your own. For example, you could make copies of the snippets above, change UIPropertyMetadata to PropertyMetadata, and add the property changed notification handler. Here is a link that will take you to all you ever wanted to know about snippets: https://msdn.microsoft.com/en-us/library/ms165392.aspx. Below is what my new snippet (added to the My Snippets folder) looks like, with changes and additions highlighted.

[Clarification: The example above is for a "regular" DependencyProperty; the modified snippet below is for an attached DependencyProperty. The principle is the same.]

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets  xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>Define an attached DependencyProperty (Silverlight)</Title>

      <Shortcut>spropa</Shortcut>

      <Description>Code snippet for an attached property using DependencyProperty as the backing store</Description>

      <Author>Microsoft Corporation</Author>

      <SnippetTypes>

        <SnippetType>Expansion</SnippetType>

      </SnippetTypes>

    </Header>

    <Snippet>

      <Declarations>

        <Literal>

          <ID>type</ID>

          <ToolTip>Property Type</ToolTip>

          <Default>int</Default>

        </Literal>

        <Literal>

          <ID>property</ID>

          <ToolTip>Property Name</ToolTip>

          <Default>MyProperty</Default>

        </Literal>

        <Literal>

          <ID>ownerclass</ID>

          <ToolTip>The owning class of this Property. Typically the class that it is declared in.</ToolTip>

        <Default>ownerclass</Default>

        </Literal>

        <Literal>

          <ID>defaultvalue</ID>

          <ToolTip>The default value for this property.</ToolTip>

          <Default>0</Default>

        </Literal>

      </Declarations>

      <Code Language="csharp">

        <![CDATA[

               

public static $type$ Get$property$(DependencyObject obj)

{

    return ($type$)obj.GetValue($property$Property);

}

public static void Set$property$(DependencyObject obj, $type$ value)

{

    obj.SetValue($property$Property, value);

}

// Using a DependencyProperty as the backing store for $property$. This enables animation, styling, binding, etc...

public static readonly DependencyProperty $property$Property =

    DependencyProperty.RegisterAttached("$property$", typeof($type$), typeof($ownerclass$), new PropertyMetadata(On$property$Changed));

private void On$property$Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)

{

}

$end$]]>

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

Comments

  • Anonymous
    June 28, 2008
    Page Brooks&#39; Controls Builder Contest, Alex Knight on Deep Zoom, Dave Relyea on Dependency Property