wpf--notes in layman's (2015.03.12)

Source: Internet
Author: User

The function of a class is simply to archive the variables and functions scattered in the program to encapsulate and control access to them. Variables encapsulated in a class are called fields (field), which represent the state of a class or instance; a function encapsulated in a class is called a method, which represents the function of a class or instance. Whether a field or method is decorated with the static keyword depends on how the field or method is meaningful to the class or on the instance of the class.

C # Language rules: Fields and methods that are meaningful to a class use the static keyword decoration, called a static member, with the class name plus access modifiers (that is, "." Operators) to access them; fields and methods that are meaningful to instances of the class do not have the static keyword, called a non-static member or instance member.

Static fields have only one copy in memory, and non-static fields have one copy per instance, regardless of whether the method is static or only one copy in memory, except that the memory that holds instructions can be accessed through the class name, or through the instance name.

Properties in the. NET framework are also known as CLR properties, either by saying that the CLR property is a secure access wrapper for private fields, or that a private field supports (back) a CLR property in the background.

The compilation of properties in C # code results in two methods and does not add to the burden of memory.

A dependency property is one that can have no value on its own and can derive a property from the data source by using a binding. Objects that have dependency properties are referred to as "dependent objects."

WPF allows an object to be created without the space used to store the object (both the space occupied by the field), the ability to get a default value when data is needed, to borrow other object data, or to allocate space in real time-an object called a dependent object (Dependency object). Its ability to acquire data in real time relies on dependency properties (Dependency property). In WPF development, you must use a dependent object as the host for the dependency property.

WPF all UI controls are dependent objects.

The host of the custom dependency property must be a derived class of dependency object. Dependency Property Instance declaration features: The reference variable is decorated with the public static readonly three modifier, and the instance is generated using the Dependencyproperty.register method.

A dependent object exists as a target of data, and when a CLR property wrapper is added for a dependency property of a dependent object, it is equivalent to preparing a binding Path for the dependent object to expose the data, that is, the dependent object has the ability to play the dual role of the data source and data target. It is worth noting that when the value of a dependency property changes, the binding object associated with it can still be notified, and the dependency property defaults to this feature.

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:

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

<summary>
The interactive logic of MainWindow.xaml
</summary>
public partial class Mainwindow:window
{
Student Stu;

Public MainWindow ()
{
InitializeComponent ();

Stu = new Student ();
Stu. SetBinding (Student.nameproperty, New Binding ("Text") {Source = this.textbox1});
Textbox2.setbinding (Textbox.textproperty, New Binding ("Name") {Source = stu});
}

private void Button_Click (object sender, RoutedEventArgs e)
{
/*student stu = new Student ();
Stu. Name = This.textBox1.Text;
This.textBox2.Text = stu. name;*/
}

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

wpf--notes in layman's (2015.03.12)

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.