-
Notifications
You must be signed in to change notification settings - Fork 807
Fix: DataGrid last column resize to view width #3417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+230
−682
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6157855
Fix: DataGrid last column resize to view width
BornToBeRoot d5743c7
Chore: Minor adjustments
BornToBeRoot 57c5852
Chore: Minor adjustments
BornToBeRoot d6571a8
Update next-release.md
BornToBeRoot 93af588
Merge branch 'main' into fix/datagrid-fill-last-row
BornToBeRoot c718c42
Potential fix for pull request finding
BornToBeRoot 353e69a
Refactor: Extract star column refresh into shared DataGridHelper atta…
Copilot 7236765
Chore: refactoring
BornToBeRoot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using System; | ||
| using System.Windows; | ||
| using System.Windows.Controls; | ||
| using System.Windows.Threading; | ||
|
|
||
| namespace NETworkManager.Utilities.WPF; | ||
|
|
||
| /// <summary> | ||
| /// Attached-property helpers for <see cref="DataGrid"/>. | ||
| /// </summary> | ||
| public static class DataGridHelper | ||
| { | ||
| /// <summary> | ||
| /// When set to <see langword="true"/>, forces star-sized columns to recompute their width after | ||
| /// the first row is realized. Without this, an initially empty <see cref="DataGrid"/> sizes star | ||
| /// columns to <c>MinWidth</c> because the inner <see cref="System.Windows.Controls.ScrollViewer"/> | ||
| /// 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. | ||
| /// </summary> | ||
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.