WPF Properties (ii) additional properties

Source: Internet
Author: User

Original: WPF attributes (ii) attached property

An attached property is a property that does not belong to an object, but is later appended because of a requirement, that is, when the object is placed in a particular environment, the properties of the object are called attached properties, and the role of the attached property is to decouple the attribute from the data type, making the design of the data type more flexible, for example, When a TextBox is placed in a different layout container, it has different layout properties, which are attached to the textbox by the layout container, and the nature of the attached property is the dependency property, which is only a little different from the registration and wrapper.

Tip, after entering Propa in VS, press TAB two times, you can add a frame with an attached property, continue pressing the TAB key, and continue to modify the contents of the attached property.


For example, person this class, put in the school will get the grade this attribute, then prepares a school class, school class inherits DependencyObject, defines an additional property

    public class School:dependencyobject    {public        static int getgrade (DependencyObject obj)        {            return (int ) obj. GetValue (Gradeproperty);        }        public static void Setgrade (DependencyObject obj, int value)        {            obj. SetValue (gradeproperty, value);        }        public static readonly DependencyProperty gradeproperty =            dependencyproperty.registerattached ("Grade", typeof ( int), typeof (School), new UIPropertyMetadata (0));    }

As you can see, the attached property has a dependency property with a difference of two points:

One. The RegisterAttached method is used by the attached property, and the dependency property uses the Register method

Two. Attached properties are packaged using two methods, and dependency properties use CLR properties to wrap the GetValue and SetValue two methods

How to use school's gradeproperty, first prepare a DependencyObject derived class human

    public class Human:dependencyobject    {    }

Use this additional property

            Human Human = new Human ();            School.setgrade (Human, 6);            MessageBox.Show (School.getgrade (human). ToString ());
Look at the example code, if you want to place a button in the middle of a table layout of 3 rows and 3 columns, the interface code is as follows:

<window x:class= "Wpfapplication1.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 showgridlines= "True" >        <Grid.RowDefinitions>            <rowdefinition/ >            <rowdefinition/>            <rowdefinition/>        </Grid.RowDefinitions>        < grid.columndefinitions>            <columndefinition/>            <columndefinition/>            < ColumnDefinition/>        </Grid.ColumnDefinitions>        <button grid.row= "1" grid.column= "1" content= " Ok "/>    </Grid></Window>

Post-run effects


So, if you use C # code to achieve this effect, the code is as follows:

            Grid Grid = new Grid () {showgridlines = true};            Grid. Rowdefinitions.add (New RowDefinition ());            Grid. Rowdefinitions.add (New RowDefinition ());            Grid. Rowdefinitions.add (New RowDefinition ());            Grid. Columndefinitions.add (New ColumnDefinition ());            Grid. Columndefinitions.add (New ColumnDefinition ());            Grid. Columndefinitions.add (New ColumnDefinition ());            Button button = New button () {Content = "OK"};            Grid.setrow (button, 1);            Grid.setcolumn (button, 1);            Grid. Children.add (button);            Content = grid;

Effect and consistency, the use of attached properties It's strange that the attached property is also a dependency property, with an example, with two sliders to control the position of the button in the nine lattice, respectively.

<window x:class= "Wpfapplication1.mainwindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation    "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "title=" MainWindow "height=" 420 "width=" 525 ">                <StackPanel> <grid showgridlines= "True" height= "+" > <Grid.RowDefinitions> <rowdefinition/> <rowdefinition/> <rowdefinition/> <                /grid.rowdefinitions> <Grid.ColumnDefinitions> <columndefinition/>            <columndefinition/> <columndefinition/> </Grid.ColumnDefinitions> <button content= "OK" grid.row= "{Binding elementname=slider1, Path=value}" grid.column= "{Binding elementname= Slider2, Path=value} "/> </Grid> <slider x:name=" slider1 "minimum=" 0 "maximum=" 2 "/> & Lt Slider x:name= "Slider2" minimum="0" maximum= "2"/> </StackPanel></Window> 


WPF Properties (ii) additional properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.