wpf DataGrid自动滚动显示最新log

<UserControl x:Class="ZASWGT.Controls.LogUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ZASWGT.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
  
    <Grid>

        <DockPanel LastChildFill="True">
            <!--<Button x:Name="btnAddLog" Content="AddLog" Click="btnAddLog_Click"  DockPanel.Dock="Top" Visibility="Visible"/>-->
            <DataGrid x:Name="dgLog"  Margin="5" AutoGenerateColumns="False"  CanUserAddRows="False" CanUserDeleteRows="False"
                   CanUserSortColumns="False" CanUserResizeColumns="True"  ItemsSource="{Binding Logs}"
                       AlternationCount="2" LoadingRow="dgLog_LoadingRow">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="{DynamicResource TimeStr}" Binding="{Binding Time}" Width="2*"/>
                    <DataGridTextColumn Header="{DynamicResource TypeStr}" Binding="{Binding Type}" Width="*"/>
                    <DataGridTextColumn Header="{DynamicResource LogStr}" Binding="{Binding Log}" Width="8*"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>

    </Grid>
</UserControl>

 

private void dgLog_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            DependencyObject obj = Utils.GetChildVisualElement(dgLog, typeof(ScrollViewer));
            if (obj==null)
            {
                return;
            }
            ScrollViewer scroll = obj as ScrollViewer;
            if (scroll!=null)
            {
                scroll.ScrollToVerticalOffset(dgLog.Items.Count - 1);
            }

        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace ZASWGT.Base
{
    public class Utils
    {
        public static string HexArrayToString(byte[] buf, int count)
        {
            List<string> lst = buf.Take(count).Select(s => s.ToString("x2")).ToList();
            string tmp = "";
            lst.ForEach(s => tmp += s + " ");
            return tmp.Trim();
        }

        public static void Swap<T>(ref T x, ref T y)
        {
            T tmp;
            tmp = x;
            x = y;
            y = tmp;
        }

        public static DependencyObject GetChildVisualElement(DependencyObject element,Type type)
        {
            DependencyObject obj = null;
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
            {
                obj = VisualTreeHelper.GetChild(element, i);
                if (obj.GetType()==type)
                {
                    break;
                }
                else
                {
                    obj = GetChildVisualElement(obj, type);
                }
            }
            return obj;
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值