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依赖于其它对象上,这就使得我的数 ...
随机推荐
- http协议与websocket协议(转)
一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...
- np.tile语法
>>> v = np.array([1, 0, 1])>>> vv = np.tile(v,(4,1))>>> print vv[[1 0 1] ...
- supervisor-program配置
[program:check_server_state]directory=/sunlight/shellcommand=/usr/bin/sh check_server_state.shautost ...
- SQLAlchemy中表结构的一对一
1.先导入相对应的库 from flask import Flask from flask_sqlalchemy import SQLAlchemy import pymysql pymysql.in ...
- java一些必会算法
经典算法的Java实现 (1)河内塔问题: 42 (2)费式数列 43 (3)巴斯卡(Pascal)三角形 44 (4)蒙地卡罗法求 PI 45 (5)最大公因数.最小公倍数 46 (6)阿姆斯壮数 ...
- 字符串转json格式
方法一:var json = JSON.parse(str)方法二:eval
- Uva 816 Abbott's Revenge(BFS)
#include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...
- 什么是 JVM
什么是 JVM 先来看下百度百科的解释: JVM 是 Java Virtual Machine(Java 虚拟机)的缩写,JVM 是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算 ...
- CentOS7.0防火墙以及开关机命令
CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态firewall-cmd --state 停止firewallsystemctl stop firewalld.service ...
- 行级锁 java||数据库
http://www.cnblogs.com/xiyubaby/p/4623516.html select * from t for update 会等待行锁释放之后,返回查询结果. select * ...