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