一、附加属性的特点
1、特殊的依赖属性
2、用于非定义该属性的类 例如Grid面板的RowDefinition、ColumnDefinition、Canvas面板的Left、Right
DockPanel面板的Dock都是附加属性。

二、附加属性的定义

1、声明数据属性变量。 public static 的DependencyProperty类型的变量。
2、在属性系统中进行注册,使用DependencyProperty.RegisterAttached()方法来注册,方法参数和注册依赖属性时Register()方法的参数一致。
3、调用静态方法设置和获取属性值。通过调用DependencyObject的SetValue()和GetValue()方法来设置和获取属性的值。
两个方法应该命名为SetPropertyName()方法和GetPropertyName()方法。

三、示例演示附加属性

实现的功能,窗体字体的大小随TextBox控件里面输入的值的大小而改变。

1、新建WPF版的用户控件,命名为“MyDependencyProperty”,在用户控件里面添加TextBox和TextBlock控件

XAML代码:

 <UserControl x:Class="WPFDependencyProperty.MyDependencyProperty"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:p="clr-namespace:WPFDependencyProperty"
mc:Ignorable="d"
d:DesignHeight="" d:DesignWidth="">
<Grid>
<StackPanel>
<TextBox p:MyDependencyProperty.MyAttachedFontSize="{Binding Path=Text,
RelativeSource={RelativeSource Mode=Self}}" ></TextBox>
<TextBlock>通过附加属性修改FontSize的大小</TextBlock>
</StackPanel>
</Grid>
</UserControl>

设计界面:

2、、在MyDependencyProperty.xaml.cs文件里面添加附件属性,附件属性的名称为MyAttachedFontSize,使用快捷方式创建附件属性:输入propa,连续按两下Tab健。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WPFDependencyProperty
{
/// <summary>
/// MyDependencyProperty.xaml 的交互逻辑
/// </summary>
public partial class MyDependencyProperty : UserControl
{
public MyDependencyProperty()
{
InitializeComponent();
} public static int GetMyAttachedFontSize(DependencyObject obj)
{
return (int)obj.GetValue(MyAttachedFontSizeProperty);
} public static void SetMyAttachedFontSize(DependencyObject obj, int value)
{
obj.SetValue(MyAttachedFontSizeProperty, value);
} // Using a DependencyProperty as the backing store for MyAttachedFontSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyAttachedFontSizeProperty =
DependencyProperty.RegisterAttached("MyAttachedFontSize", typeof(int), typeof(MyDependencyProperty),
new PropertyMetadata((s, e) =>
{
//获取MyDependencyProperty用户控件,s代表Textbox,
//MyDependencyProperty用户控件是TextBox的父级的父级的父级控件
var mdp = (((s as FrameworkElement).Parent as FrameworkElement).Parent
as FrameworkElement).Parent as MyDependencyProperty;
//更改用户控件的FontSize的值
if (mdp != null && e.NewValue != null)
{
var fontsize = ;
int.TryParse(e.NewValue.ToString(), out fontsize);
mdp.FontSize = fontsize;
}
})); }
}

3、在主界面测试附件属性

 <Window x:Class="WPFDependencyProperty.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:WPFDependencyProperty"
Title="MainWindow" Height="" Width="" WindowStartupLocation="CenterScreen">
<Grid >
<StackPanel>
<TextBlock>请输入字体的大小</TextBlock>
<p:MyDependencyProperty></p:MyDependencyProperty>
</StackPanel>
</Grid>
</Window>

程序运行效果:

在TextBox里面输入3:

在TextBox里面输入30:

WPF教程:附加属性的更多相关文章

  1. Unity3D嵌入WPF教程

    Unity3D嵌入WPF教程 创建一个 类库工程 添加 WindowForm 用户控件 (UserControl) 1).引入 UntiyWebPlayer COM 组件 在工具->选择工具箱中 ...

  2. WPF教程002 - 实现Step步骤条控件

    原文:WPF教程002 - 实现Step步骤条控件 在网上看到这么一个效果,刚好在用WPF做控件,就想着用WPF来实现一下 1.实现原理 1.1.该控件分为2个模块,类似ComboBox控件分为Ste ...

  3. WPF教程十一:简单了解并使用控件模板

    WPF教程十一:简单了解并使用控件模板 这一章梳理控件模板,每个WPF控件都设计成无外观的,但是行为设计上是不允许改变的,比如使用Button的控件时,按钮提供了能被点击的内容,那么自由的改变控件外观 ...

  4. WPF教程九:理解WPF中的对象资源

    在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源. 这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集 ...

  5. WPF教程十二:了解自定义控件的基础和自定义无外观控件

    这一篇本来想先写风格主题,主题切换.自定义配套的样式.但是最近加班.搬家.新租的房子打扫卫生,我家宝宝6月中旬要出生协调各种的事情,导致了最近精神状态不是很好,又没有看到我比较喜欢的主题风格去模仿的, ...

  6. WPF教程二:理解WPF的布局系统和常用的Panel布局

    WPF的布局系统 了解元素的测量和排列方式是理解布局的第一步.在测量(measure)阶段容器遍历所有子元素,并询问子元素它们所期望的尺寸.在排列(arrange)阶段,容器在合适的位置放置子元素.理 ...

  7. WPF Adorner+附加属性 实现控件友好提示

    标题太空泛,直接上图 无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成. 例如WPF自带的控件上要加这样的效 ...

  8. WPF利用附加属性修改ShowGridLines效果

    1.思路主要代码 wpf的gridline原本效果是虚线类型的.有时候需要设计成表格形式的,因此有了用附加属性来自动绘制边框线的想法. 思路:绘制Line并添加到grid的children里,但效果并 ...

  9. WPF通过附加属性控制窗口关闭

    场景1 当使用 ShowDialog() 方式显示窗口时,通过定义附加属性的方式可实现在 ViewModel 中进行数据绑定(bool?)来控制子窗口的显示和关闭 public class ExWin ...

随机推荐

  1. 使用anyremote进行远程鼠标控制

    源代码安装 http://anyremote.sourceforge.net/pre.html 安装  -xtest apt-get install libxtst-dev 安装 glib sudo ...

  2. Java中的设计模式

    1 单例模式和多例模式 一.单例模式和多例模式说明:1.         单例模式和多例模式属于对象模式.2.         单例模式的对象在整个系统中只有一份,多例模式可以有多个实例.(单例只会创 ...

  3. EGL接口介绍-----Android OpenGL ES底层开发

    引自:http://www.cnitblog.com/zouzheng/archive/2011/05/30/74326.html EGL 是 OpenGL ES 和底层 Native 平台视窗系统之 ...

  4. 什么是dtd文件,为什么需要

    DTD为英文Document Type Definition,中文意思为“文档类定义”.DTD肩负着两重任务:一方面它帮助你编写合法的代码,另一方面它让浏览器正确地显示器代码.也许你会问它们居然有这样 ...

  5. POJ3321/Apple tree/(DFS序+线段树)

    题目链接 Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9692 Accepted: 3217 Descr ...

  6. list遍历

     一.对List的遍历有三种方式            List<String>    list    =    new    ArrayList<String>();    ...

  7. elasticsearch高级配置之(一)----分片分布规则设置

    cluster.routing.allocation.allow_rebalance 设置根据集群中机器的状态来重新分配分片,可以设置为always, indices_primaries_active ...

  8. IDL 遍历 XML文档示例

    IDL解析XML文档同样也有2种方法:DOM和SAX方式:两种方法在IDL自带的帮助里面有详细介绍,可以去查看. IDL 源码PRO sample_recurse, oNode, indent COM ...

  9. plat模板修改记录

    每个栏目显示文章数 homepage.hph 94行 $loop = new WP_Query('post_type=post&showposts=6&category_name=xi ...

  10. JavaEE程序编码规范

    JavaEE程序编码规范 目   录 JAVA程序编码规范1 1变量的命名规则1 1.1常量(包含静态的)1 1.2类变量(静态变量)及实例变量1 1.3局部变量1 1.4参数2 1.5其它2 2方法 ...