<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;
}
}
}