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

Unable to make TextBox to auto scroll to bottom when it's being updated. #34

Open
kevwkev opened this issue Jan 26, 2024 · 6 comments
Open
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@kevwkev
Copy link

kevwkev commented Jan 26, 2024

The ScrollToEnd() function appears ineffective with TextBox, tested in both code-behind and MVVM utilizing a behavior extension of the TextBox control.

===code-hehind===

  1. Use x:name. Subscribed to TextChanged event.
  2. Call textbox.ScrollToEnd() in TextChanged event.

===behavior extension for MVVM====

public class ScrollToEndBehavior
{
    public static readonly DependencyProperty OnTextChangedProperty =
                DependencyProperty.RegisterAttached(
                "OnTextChanged",
                typeof(bool),
                typeof(ScrollToEndBehavior),
                new UIPropertyMetadata(false, OnTextChanged)
                );

    public static bool GetOnTextChanged(DependencyObject dependencyObject)
    {
        return (bool)dependencyObject.GetValue(OnTextChangedProperty);
    }

    public static void SetOnTextChanged(DependencyObject dependencyObject, bool value)
    {
        dependencyObject.SetValue(OnTextChangedProperty, value);
    }

    private static void OnTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        var textBox = dependencyObject as TextBox;
        var newValue = (bool)e.NewValue;

        if (textBox == null || (bool)e.OldValue == newValue)
        {
            return;
        }

        TextChangedEventHandler handler = (object sender, TextChangedEventArgs args) =>
            ((TextBox)sender).ScrollToEnd();

        if (newValue)
        {
            textBox.TextChanged += handler;
        }
        else
        {
            textBox.TextChanged -= handler;
        }
    }
} 
@NotYoojun
Copy link
Member

Hi! Thank you for reporting this. It seems that this is the problem made by the control ScollViewerEx.cs. I'll check that later cuz I'm in the weeds right now. 😕

@NotYoojun NotYoojun added bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed labels Jan 29, 2024
@NotYoojun
Copy link
Member

Help needed! I can't solve this after a few attempts.

@Zaitonn
Copy link
Contributor

Zaitonn commented Feb 4, 2024

https://github.com/iNKORE-Public/UI.WPF.Modern/blob/89322a8d2d41683d42e9f623ee6129ac1c46ab58/source/iNKORE.UI.WPF.Modern/Controls/ScrollViewerEx.cs#L106-L115

这段注释掉就正常了,但是不知道有无副作用
有点难评但是管用

个人感觉是ScrollInfoAdapter.SetVerticalOffset(double offset)执行动效的时候寄了

@Zaitonn
Copy link
Contributor

Zaitonn commented Feb 4, 2024

@NotYoojun
Copy link
Member

把 ScrollViewerEx 改成 ScrollViewer 的方案是不可取的。ScrollViewerEx 存在的意义就是修复和优化 WPF 自带的垃圾滚动逻辑。我们解决这个问题只能从 ScrollViewer 本身下手。

@Byolock
Copy link

Byolock commented May 7, 2024

I don't know how far your testing did go, I just realized both functions ScrollToHome and ScrollToEnd do work a single time after the application is started. Then I found out they work again after the contrary function is called, meaning a not working ScrollToEnd function does work again after ScrollToHome is called once.

In conclusion there is at least a workaround for now, just call ScrollToHome everytime before you call ScrollToEnd. It works in my test project, within a slower program or an a slower machine, adding texts fast, I would imaging this approach to be problemtic though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants