scrollbar wpf 高度_WPF TextBlock中的自动垂直滚动条?

这篇博客介绍了一种使用MVVM模式在WPF中实现文本框自动滚动到底部的方法,适用于添加日志消息的窗口。通过添加特定的XAML属性和附加行为,可以实现在每次添加新的日志消息时,窗口自动滚动到底部,同时提供了清晰的代码示例。
摘要由CSDN通过智能技术生成

这个答案描述了使用MVVM的解决方案 .

如果要向窗口添加日志框,则此解决方案非常有用,每次添加新的日志消息时,该窗口都会自动滚动到底部 .

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

添加此XAML:

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();

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切换附加属性以控制TextBox .

因为它闪电般快速 .

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值