Skip to content

Commit

Permalink
AutoSizedGridView is only adjusting the column with if the logger nam…
Browse files Browse the repository at this point in the history
…e is exceeding the last known length
  • Loading branch information
djonasdev committed Feb 10, 2020
1 parent ed1a6a2 commit f0af91a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/NLogViewer.TestApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Windows;
using DJ.Resolver;
Expand All @@ -14,24 +15,32 @@ public partial class MainWindow : Window
public const string LOREM_IPSUM = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

private readonly Logger _Logger = LogManager.GetCurrentClassLogger();
private readonly Logger _Logger2 = LogManager.GetLogger("Lorem.Ipsum.Foo.Hello.World.Lorem.Ipsum");
public MainWindow()
{
Title = $"Testing v{AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}";
InitializeComponent();
DataContext = this;

NLogViewer1.TimeStampResolver = new FooTimeStampResolver();

Stopwatch stopwatch = Stopwatch.StartNew();
Random random = new Random();
Observable.Interval(TimeSpan.FromMilliseconds(250)).ObserveOnDispatcher().Subscribe(l =>
Observable.Interval(TimeSpan.FromMilliseconds(200)).ObserveOnDispatcher().Subscribe(l =>
{
switch (random.Next(1,6))
{
case 1:
_Logger.Trace("Hello everyone");
break;
case 2:
_Logger.Debug("Hello everyone");
if (stopwatch.Elapsed.Seconds > 5)
{
_Logger2.Debug("Hello everyone");
}
else
{
_Logger.Debug("Hello everyone");
}
break;
case 3:
_Logger.Info("Hello everyone");
Expand Down
35 changes: 27 additions & 8 deletions src/NLogViewer/Helper/AutoSizedGridView.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Windows.Controls;
using System;
using System.Reactive.Linq;
using System.Windows.Controls;
using NLog;

namespace DJ.Helper
{
Expand All @@ -7,16 +10,32 @@ namespace DJ.Helper
/// Used to fix the column width: https://stackoverflow.com/questions/60147905/column-width-adjustment-is-broken-if-using-multibinding-on-displaymemberbinding
/// </summary>
public class AutoSizedGridView : GridView
{
{
private int _MaxLoggerNameLength;

protected override void PrepareItem(ListViewItem item)
{
foreach (GridViewColumn column in Columns)
if (item.DataContext is LogEventInfo info)
{
//setting NaN for the column width automatically determines the required width enough to hold the content completely.
//if column width was set to NaN already, set it ActualWidth temporarily and set to NaN. This raises the property change event and re computes the width.
if (double.IsNaN(column.Width)) column.Width = column.ActualWidth;
column.Width = double.NaN;
}
if (info.LoggerName.Length > _MaxLoggerNameLength)
{
_MaxLoggerNameLength = info.LoggerName.Length;
Observable.Timer(TimeSpan.FromMilliseconds(1)).ObserveOnDispatcher().Subscribe(l =>
{
foreach (GridViewColumn column in Columns)
{
//setting NaN for the column width automatically determines the required width enough to hold the content completely.
//if column width was set to NaN already, set it ActualWidth temporarily and set to NaN. This raises the property change event and re computes the width.
if (double.IsNaN(column.Width))
{
column.Width = column.ActualWidth;
column.Width = double.NaN;
}
}
});
}
}

base.PrepareItem(item);
}
}
Expand Down

0 comments on commit f0af91a

Please sign in to comment.