wpfTextBox⽇志⽂本框
使⽤WPF的TextBox显⽰⾃定义⽇志
TextBox中显⽰各种⽇志,随滚动条的位置不同,⽇志显⽰需要有区别: 竖直滚动条在最下⾯时,最新⽇志添加到最末⾏,并显⽰最新⽇志;竖直滚动条不在最下⾯时,最新⽇志同样添加到最末⾏,但可视区域显⽰的⽇志始终不变化位置。
滚动条在最底端
上下两张图,滚动条在最下⽅,最末⾏更新显⽰最新⽇志
滚动条不在最底端
上下两张图,滚动条在变化,但可视区域显⽰内容没有变化
⼩⼆,上代码
1)MainWindow.xaml
<Window x:Class="LogText.MainWindow"
xmlns="schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft/winfx/2006/xaml"
Title="⽇志⽂本框" Height="350" Width="200">
<Grid>
<TextBox Name="txtLog" AcceptsReturn="True"
TextWrapping="Wrap" Foreground="Green"
Grid.Row="1"Grid.ColumnSpan="2"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"/>
</Grid>
</Window>
textbox控件边框设置2)MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LogText
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(sender =>
{
while (true)
{
{
if (IsVerticalScrollBarAtBottom)
{
}
});
Thread.Sleep(600);
}
});
}
/// <summary>
/// ⽇志⽂本框滚动条是否在最下⽅
/// true:⽂本框竖直滚动条在⽂本框最下⾯时,可以在⽂本框后追加⽇志
/// false:当⽤户拖动⽂本框竖直滚动条,使其不在最下⾯时,即⽤户在查看旧⽇志,此时不添加新⽇志,
/// </summary>
public bool IsVerticalScrollBarAtBottom
{
get
{
bool atBottom = false;
{
//if (Log.VerticalScrollBarVisibility != ScrollBarVisibility.Visible)
//{
//    atBottom= true;
//    return;
//}
double dVer = Log.VerticalOffset;      //获取竖直滚动条滚动位置
double dViewport = Log.ViewportHeight;  //获取竖直可滚动内容⾼度
double dExtent = Log.ExtentHeight;      //获取可视区域的⾼度
if (dVer + dViewport >= dExtent)
{
atBottom = true;
}
else
{
atBottom = false;
}
});
return atBottom;
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。