WPF Demo14 依赖属性
using System.Windows; namespace DependencyPropertyDemo1
{
public class Student:DependencyObject
{
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name",typeof(string),typeof(Student)); // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AgeProperty =
DependencyProperty.Register("Age", typeof(int), typeof(Student)); public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
} public int Age
{
get { return (int)GetValue(AgeProperty); }
set { SetValue(AgeProperty, value); }
}
}
}
<Window x:Class="DependencyPropertyDemo1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="110,139,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,34,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,74,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
using System.Windows; namespace DependencyPropertyDemo1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student();
stu.Name = this.textBox1.Text;
this.textBox2.Text = stu.Name;
}
}
}

用法二:
using System.Windows;
using System.Windows.Data; namespace DependencyPropertyDemo2
{
public class Student:DependencyObject
{
public static readonly DependencyProperty nameProperty =
DependencyProperty.Register("Name",typeof(string),typeof(Student)); public string Name
{
get { return (string)GetValue(nameProperty); }
set { SetValue(nameProperty,value); }
} public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding)
{
return BindingOperations.SetBinding(this,dp,binding);
}
}
}
<Window x:Class="DependencyPropertyDemo2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,34,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,74,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data; namespace DependencyPropertyDemo2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); Student stu = new Student(); stu.SetBinding(Student.nameProperty, new Binding("Text") { Source = this.textBox1}); textBox2.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu});
}
}
}

WPF Demo14 依赖属性的更多相关文章
- WPF的依赖属性
Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR)属性的功能,这些服务通常统称为 WPF 属性系统.由 WPF 属 ...
- wpf 的依赖属性只能在loaded 事件之后才能取到
wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的 InitializeComponent(); 之后取不到 wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的 ...
- WPF 中依赖属性的继承(Inherits)
WPF中依赖属性的值是是可以设置为可继承(Inherits)的,这种模式下,父节点的依赖属性会将其值传递给子节点.例如,数据绑定中经常使用的DataContextProperty: var host ...
- WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性
原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? ...
- WPF的依赖属性和附加属性(用法解释较全)
转:https://www.cnblogs.com/zhili/p/WPFDependencyProperty.html 一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己 ...
- WPF利用依赖属性和命令编写自定义控件
以实例讲解(大部分讲解在代码中) 1,新建一个WPF项目,添加一个用户控件之后在用户控件里面添加几个控件用作测试, <UserControl x:Class="SelfControlD ...
- WPF: 只读依赖属性的介绍与实践
在设计与开发 WPF 自定义控件时,我们常常为会控件添加一些依赖属性以便于绑定或动画等.事实上,除了能够添加正常的依赖属性外,我们还可以为控件添加只读依赖属性(以下统称"只读属性" ...
- WPF 自定义依赖属性
原博客地址:http://www.cnblogs.com/DebugLZQ/archive/2012/11/30/2796021.html DependencyObject和Dependen ...
- [转]WPF的依赖属性是怎么节约内存的
WPF升级了CLR的属性系统,加入了依赖属性和附加属性.依赖属性的使用有很多好处,其中有两点是我认为最为亮眼的: 1)节省内存的开销; 2)属性值可以通过Binding依赖于其它对象上,这就使得我的数 ...
随机推荐
- Python 爬虫工具 —— fake_useragent
服务器为避免爬虫工具无休止的请求,以减轻负载,会对 user agent 进行校验,即判断某一 user-agent 是否不断地进行请求.可采用如下方式进行绕过服务器的校验. UserAgent_Li ...
- NSCTF2015 逆向第五题分析
这道题目我没有写出Exploit,因为编码时候里面几个细节处理出错.但对程序的逆向分析已完成,这里就学习一下别人写Exploit的思路.主要参考:绿盟科技网络攻防赛资料下载 0x01 题目要求 题目要 ...
- JavaBasic_04
选择结构 选择结构有特定的语法规则 代码要执行具体的逻辑运算进行判断(代码执行有条件) 逻辑运算的结果有两个,所以产生选择,按照不同的选择执行不同的代码(根据不同的条件执行不同的代码) Java语言提 ...
- 获取Linux服务器基本信息的shell脚本
测试运行环境: SLES12SP2 #!/bin/bash #系统名称:os_type=$(uname -o | awk '{print " | "$0}') #系统位数:32/6 ...
- linux通过安装包安装nginx和jdk
1.安装prce(重定向支持)和openssl(https支持,如果不需要https可以不安装.) yum -y install pcre* yum -y install openssl* 2.下载n ...
- Blender的单位:一图弄懂Blender的单位
1设置单位,在右边的场景设置里,可以设置公制,英制.下面还可以设置基础单位长度,就是单位网格对应的长度. 2显示单位,按N出现在右边的属性栏里,可以设置选中的显示长度及角度. 3测量,按T出现在左边d ...
- C++学习(八)(C语言部分)之 图形库
有关图形库的学习笔记 1.安装 ww.easys.cn 2.创建win32控制台应用程序 .cpp文件(图形库必须创建cpp文件) *重点 3.安装好后 重启一下vs 图形库 是一些函数的集合 作用是 ...
- MySQL Transaction--事务无法正常回滚导致的异常
问题表现:系统增删改操作明显变慢(由原来的几十毫秒变为几十秒) 查看未提交事务 ## 查看未提交的事务 ## SELECT p.ID, P.USER, P.HOST, p.DB, P.TIME, T. ...
- laravel使用过程中一些总结
推荐连接: laravel辅助函数总结:https://laravel-china.org/docs/laravel/5.5/helpers 基于 Laravel 集成的 Monolog 库对日志进行 ...
- 论 业务系统 架构 的 简化 (一) 不需要 MQ
MQ , 就是 消息队列(Message Queue), 不知从什么时候起, MQ 被用来 搭建 分布式 业务系统 架构, 一个重要作用 就是用来 “削峰” . 我们 这里 就来 讨论 如何 设 ...