Caliburn. Micro (CM) goes through the Popup binding method, caliburn. micro

Source: Internet
Author: User

Caliburn. Micro (CM) goes through the Popup binding method, caliburn. micro

Today, a friend, in the CM framework, used a Popup in the ListView emplate of a ListView and found that the methods in the VM (VM outside the set, that is, the DataContext of ListView) cannot be bound. I checked the CM source code and fixed the file here.

 

In the CM framework, bind the method. If the current DataContext does not find the corresponding method, it will traverse the DataContext of the VisualTree object until the corresponding method or traversal is found.

However, the essence of Popup is a window. It is like a self-owned VisualTree. Its root is PopupRoot. When the VisualTreeHelper. GetParent is used for PopupRoot, the return value is null.

What we need to do is to handle it when we find it is PopupRoot.

Code:

ActionMessage. SetMethodBinding = context => {var source = context. Source; DependencyObject currentElement = source; while (currentElement! = Null) {if (Action. HasTargetSet (currentElement) {var target = Message. GetHandler (currentElement); if (target! = Null) {var method = ActionMessage. GetTargetMethod (context. Message, target); if (method! = Null) {context. method = method; context. target = target; context. view = currentElement; return ;}} else {context. view = currentElement; return ;}/// modify part of Begin var pElement = VisualTreeHelper. getParent (currentElement); if (pElement = null & currentElement. getType (). name. equals ("PopupRoot", StringComparison. ordinalIgnoreCase) {var ef = currentElement as FrameworkElement; if (ef! = Null) {pElement = ef. Parent ;}} currentElement = pElement; // End} if (source! = Null & source. DataContext! = Null) {var target = source. DataContext; var method = ActionMessage. GetTargetMethod (context. Message, target); if (method! = Null) {context. Target = target; context. Method = method; context. View = source ;}}};

Only the red part is modified, and other parts are in the source code.

PS:

Because CM can only be regarded as a niche framework, a slight blind scan refers to this one, and you can skip this one.

Many methods in CM are implemented through static delegation and are still public. We can easily overwrite them without re-editing the source code. So Cool!

 

Original article

Reprinted please indicate the source: http://www.cnblogs.com/gaoshang212/p/4203929.html

 

Related Article

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.