-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[SmartHint] Improvement for multi-line TextBox hint positioning #3681
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
Conversation
Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>
TextBox.LineCount is not a DP on the TextBox control. So we add a behavior that can be attached which will listen for text changes and update the AP accordingly. This allows us to bind to TextFieldAssist.LineCount where needed. Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>
Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>
Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>
Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>
… floating hint Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>
src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs
Outdated
Show resolved
Hide resolved
| TargetType="{x:Type TextBox}" | ||
| BasedOn="{StaticResource MaterialDesignTextBoxBase}"> | ||
| <Setter Property="Padding" Value="{x:Static wpf:Constants.TextBoxDefaultPadding}" /> | ||
| <Setter Property="wpf:BehaviorsAssist.Behaviors"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably doesn't need to be addressed now, but I wonder if we should be concerned about someone using BehaviorsAssist.Behaviors to add in their own behaviors and inadvertently clearing this one. Might make for some difficult to diagnose issues later. Probably a bigger thing to address more globally though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a fair point! When thinking about it, it almost feels like the new behavior type I added should be public? That way you can at least add it back in. Would also make it easier when copy-pasting one of our styles to make certain tweaks...
|
Would it be a good idea to make the suf text aligned to the last line of the text? |
I believe we discussed this very topic when refactoring the |
|
Is the TextBox fix already in version 5.1.1-ci776? |
Yes, it should be. |
|
Installed version 5.1.1-ci776. |
Is it a text with actual "newlines" in it, or is it simply wrapping using |
|
I set VerticalContentAlignment="Top" and the hint behaved correctly. Do you plan to make the hint behave correctly with the specified HintAssist.FloatingOffset? |
If this is set explicitly, that is an opt-out of automatic positioning, and thus you need to change it yourself based on changes in height/lines/etc. So I guess the answer is no. @omllmo Can you elaborate on my questions in my previous comment, that will make it easier for me to repro locally. Thanks. |
|
Wrapped with TexWrapping=Wrap[WithOverFlow]. @nicolaihenriksen As I wrote above, setting VerticalContentAlignment="Top" solves the problem with incorrect placement of the hint. |
@omllmo That is good, but I would like it not to be necessary; I'll see if I can make it work. |
|
In another situation, incorrect behavior of a multi-line TextBox with a hint was noticed (in particular, the MaterialDesignOutlinedTextBox style):
If you do not set the value VerticalContentAligment="Top.", then the text displayed in the TextBox is located in the center, not at the top |
This is expected behavior. The default value ( By default, the text vertically aligns to the center such that it (vertically) aligns with all the other elements that could possible be there: leading icon, prefix text, suffix text, trailing icon, clear button. |
…rialDesignInXAML#3681) * Extend SmartHint demo page to reproduce multi-line issue Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com> * Expose TextBox.LineCount as an attached property TextBox.LineCount is not a DP on the TextBox control. So we add a behavior that can be attached which will listen for text changes and update the AP accordingly. This allows us to bind to TextFieldAssist.LineCount where needed. Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com> * Add AutoSuggestBox to SmartHint demo page Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com> * Modify initial vert offset converter to use lineCount in calculations Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com> * Apply LineCount AP in relevant styles Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com> * Change default RichTextBox.VerticalContentAlignment for best fit with floating hint Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com> * Align AcceptsReturn=True and TextWrapping=Wrap behaviors * Remove left-over debug code --------- Signed-off-by: Nicolai Henriksen <nicolai.h80@gmail.com>


This is intended as a partial-fix for #3675. Partial, because it only concerns the
TextBoxbased styles, while a similar issue is present with theComboBoxtemplate; I intend to do another PR to address that issue eventually.Problem
When a
TextBoxhas multiple lines of text, either viaAcceptsReturn=TrueorTextWrapping=Wrap/WrapWithOverflow, the hint position (and prefix/suffix text position) is not always correctly calculated.Example:
Proposed solution
In order to calculate the position correctly, knowledge about the number of rows/lines of text is needed. The information is available in the BCL on the
TextBoxtype (see here), but unfortunately this is not a dependency property. To make the information available via a bindable property (i.e. attached property), I have created aTextBoxLineCountBehaviorwhich hooks up toTextBox.TextChangedand effectively updatesTextFieldAssist.LineCountandTextFieldAssist.IsMultiLineaccordingly.These attached properties are then used in bindings and converters to improve the calculation of the hint position.
Example (GIF):
Changes
TextBoxLineCountBehavior,TextFieldAssist.LineCountandTextFieldAssist.IsMultiLineto support correct calculations.FloatingHintInitialVerticalOffsetConverterwhich is responsible for doing the calculation.RichTextBox.VerticalContentAlignmenttoCenterbecause this seems to be the best fit if/when you want a hint in aRichTextBox; row/line count is not really available in this control, so hint placement can be wrong depending on how you "configure" the control.Considerations
This effectively registers a
TextBox.TextChangedlistener on every single instance of aTextBox/AutoSuggestBoxin an app using the MDIX styles.Measurements
I wrote a simple WPF app simply laying out a configurable number of
TextBoxinstances in aCanvas(to avoid the overhead of aGridhaving to calculate row/columns sizes). I then measured the startup time and memory footprint (3 times and took an average value; not exactly empirical data, but meh!). The results are listed in the table below. I was looking for a linear increase which would indicate an issue, but the differences I see are so insignificant that I believe the changes in the PR do not introduce a perf cost. The fact that startup time was faster for the 2000-case also indicates that there is no issue.I ran it on my laptop with the following specs:
Intel Core Ultra 7 165H, 1400 Mhz, 16 cores, 22 logical processors, 32GB RAM