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(); } 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); + } }