diff --git a/src/Avalonia.Styling/Styling/Styles.cs b/src/Avalonia.Styling/Styling/Styles.cs index 75b8fd0c0c..690b636b1e 100644 --- a/src/Avalonia.Styling/Styling/Styles.cs +++ b/src/Avalonia.Styling/Styling/Styles.cs @@ -48,6 +48,11 @@ namespace Avalonia.Styling /// public bool TryGetResource(string key, out object value) { + if (_resources != null && _resources.TryGetValue(key, out value)) + { + return true; + } + for (var i = Count - 1; i >= 0; --i) { if (this[i].TryGetResource(key, out value)) diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs index 9e8a6a5154..f92eca25bf 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs @@ -34,8 +34,15 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions var resourceProviderType = schemaContext.GetXamlType(typeof(IResourceProvider)); var resourceProviders = ambientProvider.GetAllAmbientValues(resourceProviderType); - // Look up the ambient context for IResourceProviders which might be able to give us - // the resource. + // Look upwards though the ambient context for IResourceProviders which might be able + // to give us the resource. + // + // TODO: If we're in a template then only the ambient values since the root of the + // template wil be included here. We need some way to get hold of the parent ambient + // context and search that. See the test: + // + // StaticResource_Can_Be_Assigned_To_Property_In_ControlTemplate_In_Styles_File + // foreach (IResourceProvider resourceProvider in resourceProviders) { if (resourceProvider is IControl control && control.StylingParent != null) diff --git a/tests/Avalonia.Controls.UnitTests/ControlTests_Resources.cs b/tests/Avalonia.Controls.UnitTests/ControlTests_Resources.cs index 2f7017d52d..4fa00ab171 100644 --- a/tests/Avalonia.Controls.UnitTests/ControlTests_Resources.cs +++ b/tests/Avalonia.Controls.UnitTests/ControlTests_Resources.cs @@ -87,6 +87,30 @@ namespace Avalonia.Controls.UnitTests Assert.Equal("foo-value", target.FindResource("foo")); } + [Fact] + public void FindResource_Should_Find_Styles_Resource() + { + var target = new Control + { + Styles = + { + new Styles + { + Resources = + { + { "foo", "foo-value" }, + } + } + }, + Resources = + { + { "bar", "bar-value" }, + }, + }; + + Assert.Equal("foo-value", target.FindResource("foo")); + } + [Fact] public void FindResource_Should_Find_Application_Style_Resource() { diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StaticResourceTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StaticResourceTests.cs index b5c185d96e..e171e2ebbf 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StaticResourceTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StaticResourceTests.cs @@ -2,11 +2,15 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Linq; using Avalonia.Controls; +using Avalonia.Controls.Presenters; +using Avalonia.Controls.Templates; using Avalonia.Markup.Xaml.Data; using Avalonia.Media; using Avalonia.Styling; using Avalonia.UnitTests; +using Avalonia.VisualTree; using Xunit; namespace Avalonia.Markup.Xaml.UnitTests.Xaml @@ -62,7 +66,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml [Fact] public void StaticResource_From_Application_Can_Be_Assigned_To_Property_In_Window() { - using (StyledWindowNoTheme()) + using (StyledWindow()) { Application.Current.Resources.Add("brush", new SolidColorBrush(0xff506070)); @@ -111,7 +115,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml [Fact] public void StaticResource_Can_Be_Assigned_To_Setter() { - using (StyledWindowNoTheme()) + using (StyledWindow()) { var xaml = @" + + #ff506070 + + + +"; + + using (StyledWindow(assets: ("test:style.xaml", styleXaml))) + { + var xaml = @" + + + + + +"; + + var loader = new AvaloniaXamlLoader(); + var window = (Window)loader.Load(xaml); + var border = window.FindControl("border"); + var brush = (SolidColorBrush)border.Background; + + Assert.Equal(0xff506070, brush.Color.ToUint32()); + } + } + + [Fact(Skip = "Not yet supported by Portable.Xaml")] + public void StaticResource_Can_Be_Assigned_To_Property_In_ControlTemplate_In_Styles_File() + { + var styleXaml = @" + + + #ff506070 + + + +"; + + using (StyledWindow(assets: ("test:style.xaml", styleXaml))) + { + var xaml = @" + + + + +