This blog is about how to use attached properties and create custom attached properties.
1. Additional property usage,
There is no greater use of attached properties in WPF than setting controls on the layout of controls, such as having a rectangle, Ellipse, Button in the canvas, and we need to set their position.
<Canvas> <Rectanglex:name= "_rect"Fill= "LightBlue"Width= "+"Height= " the"Canvas.Left= "$"Canvas.Top= " the"/> <EllipseWidth= "+"Height= "+"Fill= "Lightcoral"Canvas.Left= "Max"Canvas.Top= "$"/> <ButtonContent= "I am a button"Width= "+"Height= "+"Canvas.Left= " the"Canvas.bottom= " the"/> </Canvas>
In addition to setting dependency properties in XAML, you can set them in C # code, for example:
- ); );
Display effect:
Attached properties are very simple to use.
2. Customizing additional properties
Now there is a need to rotate the button,ellipse,rectangle above a certain angle. We can do this by:
Xaml:
<Canvas> <Rectanglex:name= "_rect"Fill= "LightBlue"Width= "+"Height= " the"Canvas.Top= "+"Rendertransformorigin= ". 5,.5"> <Rectangle.rendertransform> <RotateTransformAngle= "$"/> </Rectangle.rendertransform> </Rectangle> <EllipseWidth= "Max"Height= "+"Fill= "Lightcoral"Canvas.Left= "Max"Canvas.Top= "+"Rendertransformorigin= ". 5,.5"> <Ellipse.rendertransform> <RotateTransformAngle= "$"/> </Ellipse.rendertransform> </Ellipse> <ButtonContent= "I am a button"Width= "+"Height= "+"Canvas.Left= " the"Canvas.bottom= " the"Rendertransformorigin= ". 5,.5"> <Button.rendertransform> <RotateTransformAngle= " the"/> </Button.rendertransform> </Button> </Canvas>
The effect is as follows:
Careful you have found the above three pieces of code basically the same, but also quite long. We can use additional properties to implement, create a new Rotationhelper class, the code is as follows:
usingSystem.Windows;usingSystem.Windows.Media;namespaceuseattachedproperty.helper{ Public classRotationhelper:dependencyobject { Public Static Doublegetangle (DependencyObject obj) {return(Double) obj. GetValue (Angleproperty); } Public Static voidSetangle (DependencyObject obj,Doublevalue) {obj. SetValue (angleproperty, value); } Public Static ReadOnlyDependencyProperty Angleproperty =dependencyproperty.registerattached ("Angleproperty",typeof(Double),typeof(rotationhelper),NewPropertyMetadata (0.0, onanglechanged)); Private Static voidonanglechanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {UIElement element= d asUIElement; if(Element! =NULL) {element. Rendertransformorigin=NewPoint (0.5,0.5); Element. RenderTransform=NewRotateTransform ((Double) e.newvalue); } } }}
The Rotationhelper class inherits DependencyObject so that it is not only button,ellipse, but can be used, and other elements that inherit from DependencyObject can be used.
Custom angle attached properties are now used in XAML.
<Windowx:class= "Useattachedproperty.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"Xmlns:helper= "Clr-namespace:useattachedproperty.helper"xmlns:local= "Clr-namespace:useattachedproperty"mc:ignorable= "D"Title= "MainWindow"Height= " the"Width= "525"> <Canvas> <Rectanglex:name= "_rect"Fill= "LightBlue"Width= "+"Height= " the"Canvas.Top= "+"Helper:RotationHelper.Angle= "$" /> <EllipseWidth= "Max"Height= "+"Fill= "Lightcoral"Canvas.Left= "Max"Canvas.Top= "+"Helper:RotationHelper.Angle= "$" /> <ButtonContent= "I am a button"Width= "+"Height= "+"Canvas.Left= " the"Canvas.bottom= " the"Helper:RotationHelper.Angle= " the" /> </Canvas></Window>
This displays the same effect as using RenderTransform in XAML above.
Thank you for reading. Code click here to download.
Additional properties for the WPF QuickStart series (attached property)