Visual Studio Code Snippet : Dependency Property et NotifyPropertyChange

UPDATE 1 : Pour des raisons que je ne détaillerai pas ici, certaines snippets ne référençait pas “http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet” comme xmlns. Le code et le zip est màj avec le “bon” xmlns.

Voici une série de code snippet que je me propose de partager avec vous. Elles sont souvent simples et certaines peuvent sembler un peu outdated, mais comme souvent, le developpement de snippet passe après la documentation et le nettoyage du clavier (vive les sandwich “croustillants” 😉 ). J’ai dev ces snippets au passage à VS2010 et leur migration vers VS2012 s’est déroulée avec succès, j’espère qu’elles vous seront toutes aussi utiles qu’à moi 🙂 La première c’est une snippet de Dprop pour WPF, une configProperty et les 3 autres sont des 3 styles de NotifPropChange dépendant de la version et du contexte d’utilisation. Je reviendrai certainement dans les jours qui suivent avec d’autres.

Dependency Property

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>dprop</Title>
      <Shortcut>dprop</Shortcut>
      <Description>Code snippet for dependecy property</Description>
      <Author>Emmanuel Istace</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>name</ID>
          <ToolTip>Property name</ToolTip>
          <Default>Property</Default>
        </Literal>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property Type</ToolTip>
          <Default>object</Default>
        </Literal>
        <Literal Editable="false">
          <ID>classname</ID>
          <ToolTip>Class name</ToolTip>
          <Function>ClassName()</Function>
          <Default>ClassNamePlaceholder</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
        
        public static readonly DependencyProperty $name$Property = DependencyProperty.Register("$name$", typeof($type$), typeof($classname$));

        public $type$ $name$
        {
            get { return ($type$)GetValue($name$Property); }
            set { SetValue($name$Property, value); }
        }

        $end$
]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Configuration Property

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>configprop</Title>
      <Shortcut>configprop</Shortcut>
      <Description>Add a configuration property</Description>
      <Author>Emmanuel Istace</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
          <Literal>
            <ID>Name</ID>
            <ToolTip>Property Name</ToolTip>
            <Default>MyProperty</Default>
          </Literal>    
          <Literal>
            <ID>type</ID>
            <ToolTip>Property Type</ToolTip>
            <Default>string</Default>
          </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
        [ConfigurationProperty("$Name$", IsRequired = true)]
        public $type$ $Name$
        {
          get { return ($type$)this["$Name$"]; }
          set { this["$Name$"] = value; }
        }
        $end$
]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

INotifyPropertyChanged simple implementation

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>nchangeimpl</Title>
      <Shortcut>nchangeimpl</Shortcut>
      <Description>Code snippet for INotifyPropertyChanged Simple implementation</Description>
      <Author>Emmanuel Istace</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp">
        <![CDATA[
        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
            }
        }
        #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Property Changed Event raising

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>nprop</Title>
			<Shortcut>nprop</Shortcut>
			<Description>Code snippet for property with property change event raised</Description>
			<Author>Istace Emmanuel</Author>
			<SnippetTypes>
        <SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Type exposed by the dependency property</ToolTip>
					<Default>object</Default>
				</Literal>
				<Literal>
					<ID>name</ID>
					<ToolTip>Name of the dependency property</ToolTip>
					<Default>dprop</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
				<![CDATA[
				
				#region $name$ property
				private $type$ _$name$ { get; set; }
				public $type$ $name$
				{
					get { return _$name$; }
					set
					{
					 if(value != _$name$)
					 {
					  _$name$ = value;
					  NotifyPropertyChanged("$name$");
					 }
					}
				}
				#endregion
				$end$
				
				]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

MVVM Property Changed event raising

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>mvvmlightprop</Title>
			<Shortcut>mvvmlightprop</Shortcut>
			<Description>Code snippet for MVVM view model property with property change event raised</Description>
			<Author>Istace Emmanuel</Author>
			<SnippetTypes>
        <SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Type exposed by the dependency property</ToolTip>
					<Default>object</Default>
				</Literal>
				<Literal>
					<ID>name</ID>
					<ToolTip>Name of the dependency property</ToolTip>
					<Default>dprop</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
				<![CDATA[
				
				#region $name$ property
				private $type$ _$name$ { get; set; }
				public $type$ $name$
				{
				  get { return _$name$; }
				  set
				  {
					  if(value != _$name$)
					  {
						  $type$ oldValue = _$name$;
						  _$name$ = value;
						  RaisePropertyChanged<$type$>("$name$", oldValue, value, false);
					  }
				  }
				}
				#endregion
				$end$
				]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

Télécharger l'archive contenant les snippets. (SkyDrive)Télécharger l’archive contenant les snippets. (SkyDrive)

10 thoughts on “Visual Studio Code Snippet : Dependency Property et NotifyPropertyChange

  1. Rosalie

    I constantly spent my half an hour to read this webpage’s articles or reviews everyday along with a cup of coffee.

    Reply
  2. Noel

    Hi there i am kavin, its my first time to commenting anyplace, when i read
    this article i thought i could also create comment due to this brilliant piece of writing.

    Reply
  3. Xbox 1

    fantastic publish, very informative. I wonder why the opposite specialists
    of this sector don’t notice this. You must proceed your writing. I am sure, you have a great readers’ base already!

    Reply
  4. Mattie

    Link exchange is nothing else however it is simply placing the other person’s web site link on your page at suitable place and other person will also do similar in favor of you.

    Reply
  5. jeux de monstre

    Pretty section of content. I just stumbled upon your blog and in accession capital to assert that I acquire actually enjoyed account your blog posts.

    Any way I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

    Reply
  6. Kris

    So today, I wish to look at five terrific needs to buy web hosting instead of going the 100 % totally free route.
    Many Host – Gator reviews on client aid are remarkable. Also, complimentary hosts could limit the content you place on your website.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s