WPF快速创建绑定属性

快速添加一个绑定属性,例如

private double _CurrentFontSize = 12;
public double CurrentFontSize
{
get { return _CurrentFontSize; }
set
{
_CurrentFontSize = value;
OnPropertyChanged("CurrentFontSize");
}
}

以下是步骤

1.创建一个代码段文件,文件后缀为.snippet

2.代码段为xml格式,内容如下

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Property with Notify Property Changed (Double)</Title>
      <Shortcut>propp</Shortcut>
      <Description>Creates a property with a backing field and notifies property changes, default type: double</Description>
      <Author>qjzy</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property type</ToolTip>
          <Default>double</Default> <!-- Default type changed to double -->
        </Literal>
        <Literal>
          <ID>propertyName</ID>
          <ToolTip>Property name</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
        <Literal>
          <ID>initialValue</ID>
          <ToolTip>Initial value for backing field</ToolTip>
          <Default>0.0</Default> <!-- Default initial value for double -->
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[private $type$ _$propertyName$ = $initialValue$;
        public $type$ $propertyName$
        {
            get { return _$propertyName$; }
            set
            {
                _$propertyName$ = value;
                OnPropertyChanged("$propertyName$");
            }
        }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

3.打开 Visual Studio,然后点击菜单栏中的 "工具" -> "代码片段管理器"。

4.点击添加,选择一个本地文件夹作为代码段存储位置

 5.点击导入,将刚才保存的.snippet文件导入

6.该依赖属性的快捷文本为"propp",只需输入"propp"然后按tab即可快速创建依赖属性

 

posted @ 2024-05-31 11:20  奇迹之耀  阅读(24)  评论(0编辑  收藏  举报