wpf--notes in layman's (2015.03.15)

Source: Internet
Author: User

An attached property says that a property is not part of an object, but is appended later because of a need. That is, the property that the object has after it has been placed in a particular environment (which is represented by the environment) is called an attached property (the attached attribute).

The effect of an attached property is to decouple the attribute from the data type (host), making the design of the data type more flexible. The essence of an attached property is a dependency property (Snippet:propa). Registering attached properties uses a method named RegisterAttached, and the attached property is wrapped using two methods.

Xaml:

<stackpanel background= "LightBlue" >
<textbox x:name= "TextBox1" borderbrush= "Black" margin= "5"/>
<textbox x:name= "TextBox2" borderbrush= "Black" margin= "5, 0"/>
<button content= "OK" margin= "5" click= "Button_Click"/>
</StackPanel>

C # code:

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);
}

Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc ...
public static readonly DependencyProperty Gradeproperty =
Dependencyproperty.registerattached ("Grade", typeof (int), typeof (School), new PropertyMetadata (0));
}

Class Human:dependencyobject
{

}

   //<summary>
   //MainWindow.xaml interactive logic
   // </summary>
    public partial class Mainwindow:window
    {
         public MainWindow ()
        {
            InitializeComponent ();
       }

private void Button_Click (object sender, RoutedEventArgs e)
{
Human Human = new Human ();
School.setgrade (Human, 6);
int grade = School.getgrade (human);
MessageBox.Show (grade. ToString ());
}
}

Attached properties can also use binding to rely on data on other objects:

Xaml:

<Canvas>
<slider x:name= "Sliderx" canvas.top= "ten" canvas.left= "ten" width= "260" minimum= "[maximum="]/>
<slider x:name= "Slidery" canvas.top= "ten" canvas.left= "Width=" "260" minimum= "" "Maximum="/> "
<rectangle x:name= "rect" fill= "Blue" width= "height=" "canvas.left=" {Binding Elementname=sliderx, path=value} "canvas.top=" {Binding elementname=slidery, path=value} "/>
</Canvas>

C # code equivalent to it:

This.rect.SetBinding (Canvas.leftproperty, New Binding ("Value") {Source = Sliderx});

This.rect.SetBinding (Canvas.topproperty, New Binding ("Value") {Source = slidery});

The event system evolved into a routed event in WPF, and derived a command-passing mechanism based on Routed.

Routing (route) The main idea: the starting point and the end point there are several transit stations, from the starting point after each transfer station to make a choice, and finally to the correct road to reach the end.

There are two kinds of "trees" in WPF: One is called a logical tree (Logical trees), and one is called a visual element tree (visual tree). The most notable feature of Logical tree is that it is completely composed of layout components and controls (including the entry elements in the list class control), in other words, each of its nodes is not a layout component or a control.

If you want to navigate or find elements on the logical tree, you can do so by using the static method of the Logicaltreehelper class, or if you want to navigate or find elements on the visual tree, Can be implemented using the static method of the VisualTreeHelper class.

3 key points of the event model:

The owner of the event: the sender of the message. The host of an event can fire the events it owns under certain conditions, that is, the event is triggered. The message is sent when the event is triggered.

The responder of the event: the recipient of the message, the processor. Event receivers use their event Handler to respond to events.

Subscription relationship for an event: if object A is concerned about whether an event of object B occurs, a subscribes to the event of B. The event is actually a delegate (Delegate) type member variable decorated with the event keyword, and the event handler is a function that says that a subscribes to the B event, essentially associating b.event with A.eventhandler. The so-called event excitation is called by B.event, at which point the a.eventhandler associated with it is called, and this event model is called the direct event model or the CLR event model.

This article is from the "Ink Pool small" blog, please be sure to keep this source http://306702895.blog.51cto.com/8366753/1620545

wpf--notes in layman's (2015.03.15)

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.