Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All the DependencyPropertyKey's in common elements parent classes (FrameworkElement, UIElement, ...) should be protected #10396

Open
DrkWzrd opened this issue Feb 4, 2025 · 2 comments

Comments

@DrkWzrd
Copy link

DrkWzrd commented Feb 4, 2025

When creating custom controls or framework elements, sometimes we have to handle internally things like IsMouseOver or other readonly dependency properties, inside the code behind of the control.

I don't know if there is a "proper way" to do this, but override OnDependencyPropertyChanged seems wrong to me, because I'm adding overhead for every DP change, and using the corresponding event is fine except there are some properties that doesn't have it (IsMouseOver as example).

Therefore, I'm just using a dirty trick

static ElementChild(){
        //Dirty trick
        var mouseOverPKey = (DependencyPropertyKey?)typeof(UIElement).GetField($"{nameof(IsMouseOverProperty)}Key", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)?.GetValue(null);
        mouseOverPKey?.OverrideMetadata(typeof(ElementChild), new FrameworkPropertyMetadata(OnIsMouseOverChangedStatic));

}

And I think this should be allowed by default, just doing the Key protected.

If I am in the wrong and I should do it another way, please, don't hesitate to tell me what is the "proper way" to do it.

@miloush
Copy link
Contributor

miloush commented Feb 4, 2025

Have you tried DependencyPropertyDescriptor.FromProperty(IsMouseOverProperty, typeof(MyElement)).AddValueChanged(this, OnIsMouseOverChanged);?

@DrkWzrd
Copy link
Author

DrkWzrd commented Feb 4, 2025

It works, albeit not 100% of the time, when the mouse is moved quickly between elements sometimes doesn't fire up correctly and you can be with a mouse over a element and the element had not done what it should do. Directly overriding metadata is much more responsive, I don't know why.

Any way, another API I know for now on. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants