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依赖于其它对象上,这就使得我的数 ...
随机推荐
- centos 安装thrift
Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erl ...
- Android内核sys_setresuid() Patch提权(CVE-2012-6422)
让我们的Android ROOT,多一点套路. 一.简单套路 CVE-2012-6422的漏洞利用代码,展示了另一种提权方法.(见附录) 这也是一个mmap驱动接口校验导致映射任意内核地址的洞.将内核 ...
- JsonWebToken
概述 如果各位不了解 JWT,不要紧张,它并不可怕. JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息. 让我们来假想一下一个 ...
- 实验吧—隐写术——WP之 男神一般都很低调很低调的!!
首先我们打开解题连接~ 是直接给我们下载了一个压缩文件,解压后得到: 确实是很帅的小哥哥呢,呵呵...... 分析题目提示,“获取隐藏在图片中的flag”,嗯~这句很重要,他决定了我们要先用那种工具 ...
- 【BZOJ2820】ygy的gcd
不知道为什么不想写总结,只是(因为是用别人的权限号交的所以)屯一个代码 #include<iostream> #include<cstdio> #include<algo ...
- 重拾C++第一天_WDS
1.面向对象编程的三大特点:封装.继承.多态 2.C++中若不指定类中成员的访问权限默认就是private的(class默认是private的,struct默认是public的). 3.C++规范中类 ...
- vi常用操作
什么是vi: vi是Linux/Unix底下最常用的文本编辑器,可以理解为和Windows下的txt一样,咱们一般操作linux服务器的时候都是没有图形化界面的, 怎么移动光标,到哪个位置,替换修改什 ...
- [Hi3520DV200]烧录
setenv ipaddr 192.168.1.11 setenv serverip 192.168.1.139 setenv gatewayip 192.168.1.1 mw.b ff ;sf pr ...
- docker nginx 运行后无法访问
## 1 最近在学docker部署,一开始打算将nginx先docker化的. 对照官方的docker镜像介绍说明,进行自定义配置 将官方的nginx.conf复制出来后,修改添加了一些自定义,主要是 ...
- webpack 入口:entry
定义一个入口点就生成一个chunk.如果你只是用字符串的方式定义了一个入口点,其就被命名为main.如果你用对象的方式定义多个入口点,其就被命名为入口对象中的键值.下面两个例子是等价的: entry: ...