Session (WPF Data Processing [2])

Source: Internet
Author: User
In the previous Post, I described a bunch of theoretical knowledge about Data Binding in WPF. Now, we will further analyze some of these aspects through instances.

When introducing the types of WPF Data Binding sources, the first type is any CLR object. It should be noted that, although WPF supports any CLR object, it does not work for a common CLR object class. We also need to implement a change notification mechanism on the CLR object class.

WPF encapsulates this notification mechanism in the INotifyPropertyChanged interface. As long as our CLR object class implements this interface, it has the ability to notify the customer, usually the target of notifying the binding after the property changes.

The following is a simple example to implement a Camera class that supports the notification function:

Using System;

Using System. ComponentModel;

Using System. Windows. Media. Media3D;

Namespace LYLTEST

{

Public class Camera: INotifyPropertyChanged

{

Private PerspectiveCamera m_Camera;

Public event PropertyChangedEventHandler PropertyChanged;

Public Camera ()

{

M_Camera = new PerspectiveCamera ();

}

Private void policypropertychanged (String info)

{

If (PropertyChanged! = Null)

{

PropertyChanged (this, new PropertyChangedEventArgs (info ));

}

}

Public PerspectiveCamera CameraProp

{

Get {return m_Camera ;}

Set

{

If (value! = M_Camera)

{

This. m_Camera = value;

NotifyPropertyChanged ("CameraProp ");

}

}

}

}

}

This code is very simple. First, introduce the namespace required by INotifyPropertyChanged and PerspectiveCamera used in the class. The difference between this and common CLR classes is that there is a public PropertyChangedEventHandler event type. Then, we wrap CameraProp in the. NET property to determine whether the property has changed. If yes, call another private function policypropertychanged using the current attribute name string "CameraProp. It constructs a PropertyChangedEventArgs object based on the attribute name and calls PropertyChanged. It is a notification event that should be called when attributes change.

Finally, if we need to notify that all attributes have changed, replace the preceding attribute string "CameraProp" with the parameter NULL.

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.