Skip to content

Commit 03e386c

Browse files
Fix ListView ColumnHeaders dark mode theme change.
1 parent ef1cdc3 commit 03e386c

File tree

1 file changed

+38
-25
lines changed
  • src/System.Windows.Forms/System/Windows/Forms/Controls/ListView

1 file changed

+38
-25
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,27 +1821,35 @@ public View View
18211821
throw new NotSupportedException(SR.ListViewCantSetViewToTileViewInVirtualMode);
18221822
}
18231823

1824-
if (_viewStyle != value)
1824+
if (_viewStyle == value)
18251825
{
1826-
_viewStyle = value;
1827-
if (IsHandleCreated && Application.ComCtlSupportsVisualStyles)
1828-
{
1829-
PInvokeCore.SendMessage(this, PInvoke.LVM_SETVIEW, (WPARAM)(int)_viewStyle);
1830-
UpdateGroupView();
1826+
return;
1827+
}
18311828

1832-
// if we switched to Tile view we should update the win32 list view tile view info
1833-
if (_viewStyle == View.Tile)
1834-
{
1835-
UpdateTileView();
1836-
}
1837-
}
1838-
else
1829+
_viewStyle = value;
1830+
1831+
if (IsHandleCreated && Application.ComCtlSupportsVisualStyles)
1832+
{
1833+
PInvokeCore.SendMessage(this, PInvoke.LVM_SETVIEW, (WPARAM)(int)_viewStyle);
1834+
UpdateGroupView();
1835+
1836+
// if we switched to Tile view we should update the win32 list view tile view info
1837+
if (_viewStyle == View.Tile)
18391838
{
1840-
UpdateStyles();
1839+
UpdateTileView();
18411840
}
1841+
}
1842+
else
1843+
{
1844+
UpdateStyles();
1845+
}
18421846

1843-
UpdateListViewItemsLocations();
1847+
if (IsHandleCreated && _viewStyle == View.Details)
1848+
{
1849+
ApplyColumnHeaderDarkModeOnDemand();
18441850
}
1851+
1852+
UpdateListViewItemsLocations();
18451853
}
18461854
}
18471855

@@ -4529,16 +4537,7 @@ protected override void OnHandleCreated(EventArgs e)
45294537
PInvokeCore.SendMessage(this, PInvoke.CCM_SETVERSION, (WPARAM)5);
45304538
}
45314539

4532-
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
4533-
if (Application.IsDarkModeEnabled)
4534-
{
4535-
_ = PInvoke.SetWindowTheme(HWND, $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}", null);
4536-
4537-
// Get the ListView's ColumnHeader handle:
4538-
HWND columnHeaderHandle = (HWND)PInvokeCore.SendMessage(this, PInvoke.LVM_GETHEADER, (WPARAM)0, (LPARAM)0);
4539-
PInvoke.SetWindowTheme(columnHeaderHandle, $"{DarkModeIdentifier}_{ItemsViewThemeIdentifier}", null);
4540-
}
4541-
#pragma warning restore WFO5001
4540+
ApplyColumnHeaderDarkModeOnDemand();
45424541

45434542
UpdateExtendedStyles();
45444543
RealizeProperties();
@@ -4657,6 +4656,20 @@ protected override void OnHandleCreated(EventArgs e)
46574656
}
46584657
}
46594658

4659+
private void ApplyColumnHeaderDarkModeOnDemand()
4660+
{
4661+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
4662+
if (Application.IsDarkModeEnabled)
4663+
{
4664+
_ = PInvoke.SetWindowTheme(HWND, $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}", null);
4665+
4666+
// Get the ListView's ColumnHeader handle:
4667+
HWND columnHeaderHandle = (HWND)PInvokeCore.SendMessage(this, PInvoke.LVM_GETHEADER, (WPARAM)0, (LPARAM)0);
4668+
PInvoke.SetWindowTheme(columnHeaderHandle, $"{DarkModeIdentifier}_{ItemsViewThemeIdentifier}", null);
4669+
}
4670+
#pragma warning restore WFO5001
4671+
}
4672+
46604673
protected override void OnHandleDestroyed(EventArgs e)
46614674
{
46624675
// don't save the list view items state when in virtual mode : it is the responsibility of the

0 commit comments

Comments
 (0)