有没有办法为我的类自动生成那些数据绑定就绪属性?

时间:2011-12-13 23:54:06

标签: winforms data-binding

我正在使用C#中的数据绑定编写winforms应用程序。我最近发现我写了很多属性,如下面的代码所示。编写这些代码是可以的,但我想也许我会错过一些东西,也许我可以编写更少代码并使代码看起来更干净?

这样做是否有自动化方式?
像Codedom或我不知道的任何框架?

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;    
}    

1 个答案:

答案 0 :(得分:1)

只需创建一个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>

(这与您展示的代码不完全相同,但您可以轻松地根据您的具体需求进行调整)

不可否认,代码看起来不会更清晰,但至少你会花更少的时间来编写代码;)