diff --git a/Source/NETworkManager.Utilities.WPF/DataGridHelper.cs b/Source/NETworkManager.Utilities.WPF/DataGridHelper.cs new file mode 100644 index 0000000000..1d2aaf1ba6 --- /dev/null +++ b/Source/NETworkManager.Utilities.WPF/DataGridHelper.cs @@ -0,0 +1,60 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Threading; + +namespace NETworkManager.Utilities.WPF; + +/// +/// Attached-property helpers for . +/// +public static class DataGridHelper +{ + /// + /// When set to , forces star-sized columns to recompute their width after + /// the first row is realized. Without this, an initially empty sizes star + /// columns to MinWidth because the inner + /// measures with infinite width; only a window resize would otherwise trigger a correct re-measure. + /// The handler unsubscribes itself after the first row so there is no ongoing overhead. + /// + public static readonly DependencyProperty RefreshStarColumnsOnFirstRowProperty = + DependencyProperty.RegisterAttached( + "RefreshStarColumnsOnFirstRow", + typeof(bool), + typeof(DataGridHelper), + new PropertyMetadata(false, OnRefreshStarColumnsOnFirstRowChanged)); + + public static void SetRefreshStarColumnsOnFirstRow(DataGrid element, bool value) => + element.SetValue(RefreshStarColumnsOnFirstRowProperty, value); + + public static bool GetRefreshStarColumnsOnFirstRow(DataGrid element) => + (bool)element.GetValue(RefreshStarColumnsOnFirstRowProperty); + + private static void OnRefreshStarColumnsOnFirstRowChanged(DependencyObject d, + DependencyPropertyChangedEventArgs e) + { + if (d is not DataGrid dataGrid || e.NewValue is not bool enabled || !enabled) + return; + + dataGrid.LoadingRow += Handler; + return; + + void Handler(object sender, DataGridRowEventArgs args) + { + dataGrid.LoadingRow -= Handler; + + dataGrid.Dispatcher.BeginInvoke(new Action(() => + { + foreach (var column in dataGrid.Columns) + { + if (!column.Width.IsStar) + continue; + + var width = column.Width; + column.Width = 0; + column.Width = width; + } + }), DispatcherPriority.ContextIdle); + } + } +} diff --git a/Source/NETworkManager/Views/ConnectionsView.xaml b/Source/NETworkManager/Views/ConnectionsView.xaml index b73fe87ba7..3374a8925e 100644 --- a/Source/NETworkManager/Views/ConnectionsView.xaml +++ b/Source/NETworkManager/Views/ConnectionsView.xaml @@ -11,7 +11,7 @@ xmlns:utilities="clr-namespace:NETworkManager.Utilities;assembly=NETworkManager.Utilities" xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization" xmlns:controls="clr-namespace:NETworkManager.Controls;assembly=NETworkManager.Controls" - xmlns:wpfHelper="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF" + xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF" mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:ConnectionsViewModel}"> @@ -69,7 +69,7 @@ Style="{StaticResource CleanButton}" Margin="0,0,10,0"> + wpfHelpers:ReloadAnimationHelper.IsReloading="{Binding IsRefreshing}"> @@ -95,11 +95,13 @@ Style="{StaticResource ResourceKey=SearchTextBox}" /> - + Sorting="DataGrid_OnSorting" + wpfHelpers:DataGridHelper.RefreshStarColumnsOnFirstRow="True"> + SortMemberPath="ProcessPath" MinWidth="150" + Width="*" /> @@ -84,7 +85,7 @@ + SelectedItem="{Binding QueryType}" Width="100" HorizontalAlignment="Left" />