From 69eb98e56c54c3fad9084c7be73bff7981de4737 Mon Sep 17 00:00:00 2001 From: "Aleksandar Marinov (INFRAGISTICS INC)" Date: Wed, 17 Jun 2026 20:55:33 +0300 Subject: [PATCH 1/2] Clear IsThemeDictionary flag when ResourceDictionary gains an owner Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../System/Windows/ResourceDictionary.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs index 84e2c746bdf..02ebd9fe50a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs @@ -1508,6 +1508,10 @@ internal void AddOwner(DispatcherObject owner) fe.ShouldLookupImplicitStyles = true; } + // Owned dictionaries are never theme dictionaries; clear the flag the ctor may + // have inherited from SystemResources.IsSystemResourcesParsing. Propagates to merged dictionaries. + IsThemeDictionary = false; + _ownerFEs.Add(fe); } else @@ -1530,6 +1534,10 @@ internal void AddOwner(DispatcherObject owner) fce.ShouldLookupImplicitStyles = true; } + // Owned dictionaries are never theme dictionaries; clear the flag the ctor may + // have inherited from SystemResources.IsSystemResourcesParsing. Propagates to merged dictionaries. + IsThemeDictionary = false; + _ownerFCEs.Add(fce); } else @@ -1557,6 +1565,10 @@ internal void AddOwner(DispatcherObject owner) // An Application ResourceDictionary can be accessed across threads CanBeAccessedAcrossThreads = true; + // Owned dictionaries are never theme dictionaries; clear the flag the ctor may + // have inherited from SystemResources.IsSystemResourcesParsing. Propagates to merged dictionaries. + IsThemeDictionary = false; + // Seal all the styles and templates in this app dictionary SealValues(); } From 56d94d5f17b00ec76624e89259f0348c9565e873 Mon Sep 17 00:00:00 2001 From: "Aleksandar Marinov (INFRAGISTICS INC)" Date: Thu, 18 Jun 2026 10:27:58 +0300 Subject: [PATCH 2/2] Add unit test for IsThemeDictionary flag clearing in ResourceDictionary.AddOwner Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../System/Windows/ResourceDictionaryTests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationFramework.Tests/System/Windows/ResourceDictionaryTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationFramework.Tests/System/Windows/ResourceDictionaryTests.cs index 492da8764c5..2af54568a1b 100644 --- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationFramework.Tests/System/Windows/ResourceDictionaryTests.cs +++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationFramework.Tests/System/Windows/ResourceDictionaryTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Linq; +using System.Reflection; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Threading; @@ -159,4 +160,19 @@ public void DynamicResource_ShouldRemainLastValue_WhenLocalResourceRemoved() // The default foreground color of textBlock (as in aero2) textBlock.Foreground.Should().BeSameAs(Brushes.Black); } + + [WpfFact] + public void AddOwner_ClearsInheritedIsThemeDictionaryFlag() + { + // Simulate a ResourceDictionary whose ctor inherited + // IsThemeDictionary from SystemResources.IsSystemResourcesParsing. + PropertyInfo isThemeDictionary = typeof(ResourceDictionary).GetProperty( + "IsThemeDictionary", BindingFlags.Instance | BindingFlags.NonPublic)!; + var rd = new ResourceDictionary(); + isThemeDictionary.SetValue(rd, true); + + _ = new Button { Resources = rd }; + + isThemeDictionary.GetValue(rd).Should().Be(false); + } }