Manipulating ZIndex of Adorners to change click detection between controls
Hey, I have a Canvas with a bunch of ContentControls added as children. Each ContentControl has an adorner on top to detect clicks on the ContentControl (plus other functions). I would like to change the ZIndex of the ContentControl and so change which ContentControl detects the click. I changed the ZIndex of the adorner, but when I click the ContentControl that looks to be on top (higher ZIndex), the click is detected by the control in the back, which is the way it was before the reordering of the ZIndex.
So, how can I solve this?
This is the code I use to change the ZIndex of the Adorner:
Panel.SetZIndex(mSelectionAdorner, (int)RegionContent.ZIndex);
mSelectionAdorner is a pr开发者_运维知识库ivate reference to the adorner that sits on top of the ContentControl.
Thanks.
Panel.ZIndex cannot be used for ordering of adorners for some reason. The relative ZIndex of the adorned controls does not matter to the order of the adorners either, because the adorners are lifted and put in an adornerlayer that lies on top of all your actual controls.
I had a similar problem, but just needed a specific ordering of my adorners. I ended up using reflection to set the orders:
var setZOrderMethodInfo = adornerLayer.GetType().GetMethod("SetAdornerZOrder",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
setZOrderMethodInfo.Invoke(adornerLayer, new object[] { adorner1, 0 });
setZOrderMethodInfo.Invoke(adornerLayer, new object[] { adorner2, 1 });
from ZOrder of Adorners in AdornerLayer.
精彩评论