2011-12-13 15 views
1

私は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

だけ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> 

(これはあなたが示したコードと全く同じではありませんが、あなたが簡単にあなたの正確なニーズに適応させることができます)

確かに、コードはきれいに見えませんが、少なくともあなたはそれを書いて時間を節約します;)

関連する問題