ChatGPT解决这个技术问题 Extra ChatGPT

WPF TextBlock中的自动垂直滚动条?

我在 WPF 中有一个 TextBlock。我给它写了很多行,远远超过了它的垂直高度。我希望在发生这种情况时会自动出现一个垂直滚动条,但事实并非如此。我试图在“属性”窗格中查找滚动条属性,但找不到。

一旦其内容超过其高度,如何为我的 TextBlock 自动创建垂直滚动条?

澄清:我宁愿从设计者那里完成,而不是直接写入 XAML。

重新阅读此问题后,我注意到您提到 TextBlock 两次和 TextBox 一次。

D
Drew Noakes

将其包装在滚动查看器中:

<ScrollViewer>
    <TextBlock />
</ScrollViewer>

注意此答案适用于原始问题中要求的 TextBlock(只读文本元素)。

如果您想在 TextBox(可编辑的文本元素)中显示滚动条,请使用 ScrollViewer 附加属性:

<TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ScrollViewer.VerticalScrollBarVisibility="Auto" />

这两个属性的有效值为 DisabledAutoHiddenVisible


我如何从设计师那里做到这一点?
抱歉,我不确定,我不使用 WPF 设计器。我认为如果您直接添加 XAML,设计器将自行更新。
@conqenator TextBox.ScrollToEnd();
@Greg,问题是关于 TextBlock 而不是 TextBox
如果封闭元素不强制任何高度,有时需要 Scrollviewer 上的 MaxHeight 来强制显示 scoll。
a
akjoshi

现在可以使用以下内容:

<TextBox Name="myTextBox" 
         ScrollViewer.HorizontalScrollBarVisibility="Auto"
         ScrollViewer.VerticalScrollBarVisibility="Auto"
         ScrollViewer.CanContentScroll="True">SOME TEXT
</TextBox>

@jjnguy,我将最初的问题解释为关于 TextBlock 而不是 TextBox(如标题和开头行),但第二段提到了 TextBox。需要明确的是,这个答案绝对是文本 boxes 的最佳方法,而我的答案是我所知道的文本 blocks 的最佳方法 :)
@Drew,啊,有道理。感谢您的澄清。
对我来说也更好。至少对于 TextBox,当在它周围使用 ScrollViewer 时,就像在接受的答案中一样,TextBox 的边框消失了,因为整个控件都被滚动了,而不仅仅是它的内容。
J
Josh

更好的是:

<Grid Width="Your-specified-value" >
    <ScrollViewer>
         <TextBlock Width="Auto" TextWrapping="Wrap" />
    </ScrollViewer>
</Grid>

这可以确保文本块中的文本不会溢出并与文本块下方的元素重叠,如果您不使用网格,可能会出现这种情况。当我尝试其他解决方案时,这发生在我身上,即使文本块已经在与其他元素的网格中。请记住,文本块的宽度应该是 Auto 并且您应该在 Grid 元素中指定所需的宽度。我在我的代码中做到了这一点,它工作得很好。 HTH。


Ω
ΩmegaMan
<ScrollViewer MaxHeight="50"  
              Width="Auto" 
              HorizontalScrollBarVisibility="Disabled"
              VerticalScrollBarVisibility="Auto">
     <TextBlock Text="{Binding Path=}" 
                Style="{StaticResource TextStyle_Data}" 
                TextWrapping="Wrap" />
</ScrollViewer>

我通过将 MaxHeight 放入 ScrollViewer 以另一种方式执行此操作。

只需调整 MaxHeight 以显示更多或更少的文本行。简单的。


D
Drew Noakes
<ScrollViewer Height="239" VerticalScrollBarVisibility="Auto">
    <TextBox AcceptsReturn="True" TextWrapping="Wrap" LineHeight="10" />
</ScrollViewer>

这是在 XAML 中使用滚动文本框并将其用作文本区域的方法。


该问题与 TextBlock 而不是 TextBox 有关。
不是很正确的答案,但我发现 VerticalScrollBarVisibility 是一个有用的提示,所以 +1
R
Rob

您可以使用

ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible"

这些是 wpf 的附加属性。了解更多信息

http://wpfbugs.blogspot.in/2014/02/wpf-layout-controls-scrollviewer.html


C
Contango

此答案描述了使用 MVVM 的解决方案。

如果您想在窗口中添加一个日志框,这个解决方案非常棒,每次添加新的日志消息时,它都会自动滚动到底部。

一旦添加了这些附加属性,它们就可以在任何地方重用,因此它是非常模块化和可重用的软件。

添加此 XAML:

<TextBox IsReadOnly="True"   
         Foreground="Gainsboro"                           
         FontSize="13" 
         ScrollViewer.HorizontalScrollBarVisibility="Auto"
         ScrollViewer.VerticalScrollBarVisibility="Auto"
         ScrollViewer.CanContentScroll="True"
         attachedBehaviors:TextBoxApppendBehaviors.AppendText="{Binding LogBoxViewModel.AttachedPropertyAppend}"                                       
         attachedBehaviors:TextBoxClearBehavior.TextBoxClear="{Binding LogBoxViewModel.AttachedPropertyClear}"                                    
         TextWrapping="Wrap">

添加此附加属性:

public static class TextBoxApppendBehaviors
{
    #region AppendText Attached Property
    public static readonly DependencyProperty AppendTextProperty =
        DependencyProperty.RegisterAttached(
            "AppendText",
            typeof (string),
            typeof (TextBoxApppendBehaviors),
            new UIPropertyMetadata(null, OnAppendTextChanged));

    public static string GetAppendText(TextBox textBox)
    {
        return (string)textBox.GetValue(AppendTextProperty);
    }

    public static void SetAppendText(
        TextBox textBox,
        string value)
    {
        textBox.SetValue(AppendTextProperty, value);
    }

    private static void OnAppendTextChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs args)
    {
        if (args.NewValue == null)
        {
            return;
        }

        string toAppend = args.NewValue.ToString();

        if (toAppend == "")
        {
            return;
        }

        TextBox textBox = d as TextBox;
        textBox?.AppendText(toAppend);
        textBox?.ScrollToEnd();
    }
    #endregion
}

这个附加属性(清除框):

public static class TextBoxClearBehavior
{
    public static readonly DependencyProperty TextBoxClearProperty =
        DependencyProperty.RegisterAttached(
            "TextBoxClear",
            typeof(bool),
            typeof(TextBoxClearBehavior),
            new UIPropertyMetadata(false, OnTextBoxClearPropertyChanged));

    public static bool GetTextBoxClear(DependencyObject obj)
    {
        return (bool)obj.GetValue(TextBoxClearProperty);
    }

    public static void SetTextBoxClear(DependencyObject obj, bool value)
    {
        obj.SetValue(TextBoxClearProperty, value);
    }

    private static void OnTextBoxClearPropertyChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs args)
    {
        if ((bool)args.NewValue == false)
        {
            return;
        }

        var textBox = (TextBox)d;
        textBox?.Clear();
    }
}   

然后,如果您使用的是 MEF 等依赖注入框架,您可以将所有特定于日志记录的代码放入它自己的 ViewModel 中:

public interface ILogBoxViewModel
{
    void CmdAppend(string toAppend);
    void CmdClear();

    bool AttachedPropertyClear { get; set; }

    string AttachedPropertyAppend { get; set; }
}

[Export(typeof(ILogBoxViewModel))]
public class LogBoxViewModel : ILogBoxViewModel, INotifyPropertyChanged
{
    private readonly ILog _log = LogManager.GetLogger<LogBoxViewModel>();

    private bool _attachedPropertyClear;
    private string _attachedPropertyAppend;

    public void CmdAppend(string toAppend)
    {
        string toLog = $"{DateTime.Now:HH:mm:ss} - {toAppend}\n";

        // Attached properties only fire on a change. This means it will still work if we publish the same message twice.
        AttachedPropertyAppend = "";
        AttachedPropertyAppend = toLog;

        _log.Info($"Appended to log box: {toAppend}.");
    }

    public void CmdClear()
    {
        AttachedPropertyClear = false;
        AttachedPropertyClear = true;

        _log.Info($"Cleared the GUI log box.");
    }

    public bool AttachedPropertyClear
    {
        get { return _attachedPropertyClear; }
        set { _attachedPropertyClear = value; OnPropertyChanged(); }
    }

    public string AttachedPropertyAppend
    {
        get { return _attachedPropertyAppend; }
        set { _attachedPropertyAppend = value; OnPropertyChanged(); }
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

以下是它的工作原理:

ViewModel 切换附加属性以控制文本框。

因为它使用“追加”,所以速度很快。

任何其他 ViewModel 都可以通过调用日志 ViewModel 上的方法来生成日志消息。

当我们使用 TextBox 中内置的 ScrollViewer 时,我们可以让它在每次添加新消息时自动滚动到文本框的底部。


S
Scott Bordelon

我试图让这些建议适用于文本块,但无法让它发挥作用。我什至试图让它从设计师那里工作。 (查看布局并通过单击底部的向下箭头“V”来展开列表)我尝试将滚动查看器设置为可见,然后设置为自动,但它仍然无法正常工作。

我最终放弃了并将 TextBlock 更改为具有 Readonly 属性集的 TextBox,它就像一个魅力。


Z
Zuhair

这是该问题的简单解决方案。只有在文本溢出时才会激活垂直滚动。

<TextBox Text="Try typing some text here " ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="WrapWithOverflow" />


Ω
ΩmegaMan

不知道其他人是否有此问题,但将我的 TextBlock 包装到 ScrollViewer 中以某种方式弄乱了我的 UI - 作为一种简单的解决方法,我发现将 TextBlock 替换为像这样的 TextBox

<TextBox  SelectionBrush="Transparent" 
          Cursor="Arrow" 
          IsReadOnly="True" 
          Text="{Binding Text}" 
          VerticalScrollBarVisibility="Auto">

创建一个外观和行为类似于带有滚动条的 TextBlockTextBox(您可以在设计器中完成这一切)。