开发者

How to determine on which UIElement a tagged object is placed?

开发者 https://www.devze.com 2023-01-23 03:31 出处:网络
In my app I use tagged objects. Now I\'d like to react differently not only on the object that is placed on the surface, but also on the el开发者_JAVA技巧ement it is placed on. Is this possible someho

In my app I use tagged objects. Now I'd like to react differently not only on the object that is placed on the surface, but also on the el开发者_JAVA技巧ement it is placed on. Is this possible somehow? I couldn't find any information about it.


You might do a HitTest. Basically you get the point relative to your window and look which element of the visual tree is being hit. No code sample at hand, but you'll find a lot using HitTest and WPF on google.


Yes, it is possible. Please show us your code so we can help you better.

What you are looking for is probably Reflection.

Did you know you can always check if an object is of a certain type with the is operator?

var tag = myDependencyObject.Tag;
if(myDependencyObject is CheckBox)
{
    //...
}
else if(myDependencyObject is TextBox)
{
    //...
}

To detect a change of the Tag-Property, listen to the DependencyPropertyChanged event like this:

DependencyPropertyDescriptor prop = DependencyPropertyDescriptor.FromProperty(
    FrameworkElement.TagProperty,
    typeof(FrameworkElement));

prop.AddValueChanged(aTaggedControl, this.YourEventHandlerMethod);
0

精彩评论

暂无评论...
验证码 换一张
取 消