C
C#•3y ago
Anton

WPF Tooltips in validation templates

The solutions here https://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation here https://stackoverflow.com/a/14550973/9731532 here https://social.msdn.microsoft.com/Forums/vstudio/en-US/d54e66da-8500-40a2-bc41-42e661043420/how-to-implement-tooltips-for-validation-errors-and-still-have-tooltips?forum=wpf all create a tooltip to show validation errors by setting a style trigger. Is there a standard way to incorporate the tooltip into the validation template, without ruining anything? The issue with styles is that a control can only have a single style, you can't "absorb" the props defined in multiple styles. How would I add common styles if I already used my only style to add the tooltip? It doesn't feel right. The invisible overlay in one of the answers is a really terrible idea, because it would consume clicks (this is what I mean by ruining something).
CodeProject
Validation in Windows Presentation Foundation
In this article, I'll walk you through using the built-in Validation classes that exist in Windows Presentation Foundation. I'll then discuss an alternative approach to validation that might suit a richer domain layer, by creating a custom WPF ErrorProvider.
Stack Overflow
WPF Validation Errors: Setting Tooltip with Error Message
Why is there no tooltip text on errors? <Style TargetType="{x:Type TextBox}"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTempl...
26 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Anton
AntonOP•3y ago
I guess the most correct solution would be: 1. Store the original tooltip in a custom property; 2. When validation of the binding gets done, if there are errors, set the tooltip to the error, otherwise set the tooltip to the original tooltip; 3. Intercept setting the tooltop property, resetting the original tooltip instead, and restoring back the error if the current state is that there's an error. This would work always, given there exist the mentioned events But I fear it's too ad hoc, it disregards any mechanisms that might already exist in the framework (I'm new to it and just not aware of them) Can someone who has more insight into the internals give me a hint here?
230V
230V•3y ago
@WPF
Starlk
Starlk•3y ago
I don't understand the question, are you trying to design a tooltip that shows validation errors when needed and also the default content when needed?
Anton
AntonOP•3y ago
Not design a tooltip, reset the tooltip of a control. - if there are no errors, it should show the tooltip that has been set for the control; - if there are errors, it should display said errors. This should ideally be a reusable piece of logic that I could apply to any control without rewriting it. At least that's how I see it
Starlk
Starlk•3y ago
Hmm I see. I personally would create an attached property that's called something like "Foo.Tooltip" then in the logic of the attached property I'd get the control, check if it has errors if it does I'd change the content of the tooltip to display the errors, and if it doesn't then the normal default tooltip will be shown
Anton
AntonOP•3y ago
I didn't know attached properties could have logic So does that mean I could react to when the validation content of the binding changes?
Starlk
Starlk•3y ago
Uh that's a good point, I don't really know a way to notify the attached property of the validation changes 🤔
SuperBrain
SuperBrain•3y ago
Are you sure Tooltips are a good way of displaying validation errors? 👀
Anton
AntonOP•3y ago
I don't know, are they?
SuperBrain
SuperBrain•3y ago
Can you show us how this "tooltip" would look if it was working the way you want it to work? Tooltip are like popups - they're shown either for a specific duration or until clicked anywhere. They're not really suited for showing validation errors, especially if you want to show them on multiple controls at the same time, simply because ... well .. there can be only 1 Tooltip visible at any time (by default).
Anton
AntonOP•3y ago
A box with the error would pop up at the cursor position if it's hovered within the control's bounding box, at the cursor position, or whatever is the position that tooltips usually appear (do they try to position themselves such that they go off the bounding box the least amount possible?) yeah, well, if hovered
SuperBrain
SuperBrain•3y ago
IMO, that's not a good idea. Have you seen how validation errors are displayed in web pages (in most cases)?
Anton
AntonOP•3y ago
Below or to the side, I presume
SuperBrain
SuperBrain•3y ago
Typically, they're just labels, hidden by default. In WPF, there are a few different ways to utilize this. And none of these solutions use Tooltips 👀
Anton
AntonOP•3y ago
You can't do that with validation templates though, you can attach them to the side, but they can't flow normally I'd make a separate column in the grid just for the validation errors, but I apparently can't
SuperBrain
SuperBrain•3y ago
WPF has something called Adorners. They're perfectly suited for displaying validation errors.
Anton
AntonOP•3y ago
Don't they use an overlay? How could I position them within a grid, in a column, normally? Should I try searching for this thing?
SuperBrain
SuperBrain•3y ago
Adorners are control-bound As in, they're positioned relative to a control they relate to. And yes, you should definitely look into this as Adorners can be used for a lot of different things. https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/adorners-overview
Anton
AntonOP•3y ago
From what I've read on them before now, is that they draw themselves on a separate layer
SuperBrain
SuperBrain•3y ago
That is correct.
Anton
AntonOP•3y ago
I'd like to allocate a column in a grid just for the errors, so that I don't have to manually size or wrap them or whatnot. Let me see what I can find on making them follow regular layout.
Anton
AntonOP•3y ago
thank you
bill gate
bill gate•3y ago
bruh
Want results from more Discord servers?
Add your server