AZ.
AZ.

Reputation: 7573

Is there a way to auto generate those data-binding ready properties for my class?

I'm writing a winforms application using data-binding in C#. I found recently I'm writing a lot of properties like the code shown at below. It's ok to write those code but I'm thinking maybe I miss something, maybe I could code less and make the code look cleaner?

Is there automated way for doing this?
Like Codedom or any framework I don't know?

public class SampleClass : INotifyPropertyChanged
{
    public Boolean Enabled
    {
        get { return _enabled; }
        set
        {
            if (_enabled == value) return;

            _enabled = value;

            // broadcast the change
            RaisePropertyChanged(PropertyName_Enabled);

            // this object is modified
            this.Modified = true;
        }
    }

    public Single Degree
    {
        get { return _degree; }
        set
        {
            if (_degree == value) return;

            _degree = value;

            // broadcast the change
            RaisePropertyChanged(PropertyName_Degree);

            // this object is modified
            this.Modified = true;
        }
    }

    // Define the property name this class exposes and notifies
    public static readonly String PropertyName_Enabled = "Enabled";
    public static readonly String PropertyName_Degree = "Degree";

    private Boolean _enabled;
    private Single _degree;    
}    

Upvotes: 1

Views: 105

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292445

Just create a code snippet:

<?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>propnot</Title>
      <Author>Thomas Levesque</Author>
      <Description>Code snippet for property with change notification</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>propnot</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>type</ID>
          <ToolTip>Property type</ToolTip>
          <Default>int</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>property</ID>
          <ToolTip>Property name</ToolTip>
          <Default>MyProperty</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>field</ID>
          <ToolTip>The variable backing this property</ToolTip>
          <Default>myProperty</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>notifyMethod</ID>
          <ToolTip>The method used to notify the listeners</ToolTip>
          <Default>OnPropertyChanged</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[private $type$ $field$;
public $type$ $property$
{
    get { return $field$;}
    set
    {
        $field$ = value;
        $notifyMethod$("$property$");
    }
}
$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

(this is not exactly the same as the code you shown, but you can easily adapt it to your exact needs)

Admittedly, the code won't look cleaner, but at least you will spend less time writing it ;)

Upvotes: 1

Related Questions