55using Microsoft . UI . Xaml . Controls ;
66using Microsoft . UI . Xaml . Data ;
77using MsGraphSamples . WinUI . Converters ;
8+ using MsGraphSamples . WinUI . Helpers ;
89using MsGraphSamples . WinUI . ViewModels ;
910using System . Collections . Immutable ;
11+ using System . Reflection ;
12+ using Windows . Foundation ;
1013
1114namespace MsGraphSamples . WinUI . Views ;
1215public sealed partial class MainPage : Page , IRecipient < ImmutableSortedDictionary < string , DataGridSortDirection ? > >
1316{
1417 public MainViewModel ViewModel { get ; } = Ioc . Default . GetRequiredService < MainViewModel > ( ) ;
18+ private readonly Debouncer debouncer = new ( TimeSpan . FromMilliseconds ( 600 ) ) ;
1519
1620 public MainPage ( )
1721 {
@@ -54,4 +58,26 @@ private void TextBox_SelectAll(object sender, RoutedEventArgs _)
5458 var textBox = ( TextBox ) sender ;
5559 textBox . SelectAll ( ) ;
5660 }
61+
62+ private void DirectoryObjectsGrid_SizeChanged ( object sender , SizeChangedEventArgs e )
63+ {
64+ debouncer . Debounce ( SetPageSize ) ;
65+ }
66+
67+ private void SetPageSize ( )
68+ {
69+ var rowsPresenterAvailableSizeObj = typeof ( DataGrid )
70+ . GetField ( "_rowsPresenterAvailableSize" , BindingFlags . NonPublic | BindingFlags . Instance )
71+ ? . GetValue ( DirectoryObjectsGrid ) ;
72+
73+ var rowHeightEstimateObj = typeof ( DataGrid )
74+ . GetProperty ( "RowHeightEstimate" , BindingFlags . NonPublic | BindingFlags . Instance )
75+ ? . GetValue ( DirectoryObjectsGrid ) ;
76+
77+ if ( rowsPresenterAvailableSizeObj is Size rowPresenterSize &&
78+ rowHeightEstimateObj is double rowHeight )
79+ {
80+ ViewModel . PageSize = ( ushort ) Math . Min ( Math . Round ( DirectoryObjectsGrid . DataFetchSize * rowPresenterSize . Height / rowHeight ) , 999 ) ;
81+ }
82+ }
5783}
0 commit comments