当前位置:网站首页>WPF dependent properties
WPF dependent properties
2022-06-21 22:34:00 【zLulus】
Concept
A dependent attribute is one that has no value , adopt Binding Get value from data source , It's relying on other people , Objects with dependent properties are called dependent objects
Scenarios where dependent attributes are applied
1、 Set properties in the style .
2、 Property supports data binding .
3、 Use dynamic resource references to set properties .
4、 Automatically inherit attribute values from the parent element in the element tree .
5、 Attributes can be animated .
6、 The attribute system is in the attribute system 、 Reports when an environment or user performs an action or reads and changes the previous value of a property using a style .
7、 Use the established 、WPF Processes also use metadata conventions ,
For example, report whether the layout system is required to rewrite the visual object of an element when changing attribute values . Dependent objects are created without a storage data space .WPF You must use the dependent object as the host of the dependent property in .
For data binding
Dependent attributes are static attributes for a class
Each class instance goes to this static attribute Get The attribute that belongs to you
Objects have properties , Property to the public data area 【 The dependent properties of all class instances are placed in a static variable ( Public area ) Inside 】 Take inside
advantage : The public data area is not UI Threads , So you can change the value at any time
Tips
WPF Interface elements are dependent objects , All attributes are dependent , So binding is supported 
Example
Custom control
public class CustomBorder : Border
{
public CustomBorder()
{
// A background color is given by default during initialization
Background = Brushes.Blue;
}
// attribute
public Double Transparency
{
get { return (Double)GetValue(TransparencyDependency); }
set { SetValue(TransparencyDependency, value); }
}
// Register dependency properties
public readonly static DependencyProperty TransparencyDependency =
DependencyProperty.Register(
// Property name
"Transparency",
// Property data type
typeof(Double),
// The owner
typeof(CustomBorder),
// processing method
new PropertyMetadata(new PropertyChangedCallback(transparencyPropertyChangedCallback))
);
// Processing when the attribute is modified
static void transparencyPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
CustomBorder border = (sender as CustomBorder);
if (border != null)
{
border.Opacity = 1 - Convert.ToDouble(e.NewValue) / 255;
}
}
}
DependencyProperty Of Register Method
There are several overloading methods , Here are two 
//
// Abstract :
// Use the specified attribute name 、 Attribute types 、 Owner type and attribute metadata register dependent attributes .
//
// Parameters :
// name:
// The name of the dependent property to register .
//
// propertyType:
// Type of attribute .
//
// ownerType:
// Registering owner type of dependent property .
//
// typeMetadata:
// Attribute metadata that depends on attributes .
//
// Return results :
// A dependent attribute identifier , You should use it to set the public static readonly Value of field . This identifier will be used later to refer to dependent properties , Thus, operations such as setting its value or obtaining metadata in a programmatic manner can be realized .
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);
The front desk
<Grid>
<local:CustomBorder x:Name="bord" Transparency="{Binding ElementName=slider1, Path=Value}"></local:CustomBorder>
<Slider Grid.Row="1" HorizontalAlignment="Left" Name="slider1" Width="130" Value="0" Minimum="0" Maximum="255" />
</Grid>
Sample code
边栏推荐
- tkinter中text文本与scroll滚动条关联
- class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist
- Implement a middleware from -1
- Fedora 36 编译安装opencv 4.6.0 —— 筑梦之路
- Correspondence between rtx3090 and pytorch versions
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- Leetcode question brushing: SF Technology Smart logistics Campus Technology Challenge
- Learn vector -- how to use common interfaces
- Use the while loop to calculate the odd and even sums in 1-100 [method 2]
- 力扣:零钱兑换
猜你喜欢

Introduction to software architecture

力扣刷題集結4(mysql版本)
![class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist](/img/1a/294eb0128285686ede415991f69be7.png)
class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist

【深入理解TcaplusDB技术】单据受理之表管理

LeetCode-543-二叉树的直径

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

Hiclotter|hic data visualization tool

About Eureka starting successfully but accessing 404

翻译软件Bob安装教程

Jeu de boutons de force 4 (version MySQL)
随机推荐
实践 DevOps 时,可能面临的六大挑战
迅为iTOP-3568开发板安装 RKNN Toolkit Lite2
Use for, while and do while to find the sum of 1-100 circularly
Anaconda add channels
JMter测试命令【笔记】
Jeu de boutons de force 4 (version MySQL)
flutter系列之:flutter中的IndexedStack
专业字体设计编辑Glyphs 3
FPGA之道——数字系统之间的接口电平标准
力扣刷题集结4(mysql版本)
WPF 启动带参数
力扣刷題集結4(mysql版本)
利用do while循环,分别计算1-100中奇数的和、偶数的和【方法一】
力扣:零钱兑换
[deeply understand tcapulusdb technology] tmonitor system upgrade
WPF 路由
FPGA设计中RAM和ROM初始化的方法
GDB调试实战(8)给程序传递启动参数
Second understanding microservice
企业微信内置应用h5开发记录-1