11 changed files with 269 additions and 5 deletions
@ -0,0 +1,33 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using OmniXaml; |
|||
using Perspex.LogicalTree; |
|||
using Perspex.Styling; |
|||
|
|||
namespace Perspex.Markup.Xaml.MarkupExtensions |
|||
{ |
|||
public class StyleResourceExtension : MarkupExtension |
|||
{ |
|||
public StyleResourceExtension(string name) |
|||
{ |
|||
Name = name; |
|||
} |
|||
|
|||
public override object ProvideValue(MarkupExtensionContext extensionContext) |
|||
{ |
|||
var styleHost = extensionContext.TargetObject as IStyleHost; |
|||
|
|||
if (styleHost == null) |
|||
{ |
|||
throw new ParseException( |
|||
$"StyleResource cannot be assigned to an object of type '{styleHost.GetType()}'."); |
|||
} |
|||
|
|||
return styleHost.FindStyleResource(Name); |
|||
} |
|||
|
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
|
|||
namespace Perspex.Styling |
|||
{ |
|||
public static class StyleExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Tries to find a named style resource.
|
|||
/// </summary>
|
|||
/// <param name="control">The control from which to find the resource.</param>
|
|||
/// <param name="name">The resource name.</param>
|
|||
/// <returns>
|
|||
/// The resource if found, otherwise <see cref="PerspexProperty.UnsetValue"/>.
|
|||
/// </returns>
|
|||
public static object FindStyleResource(this IStyleHost control, string name) |
|||
{ |
|||
Contract.Requires<ArgumentNullException>(control != null); |
|||
Contract.Requires<ArgumentNullException>(name != null); |
|||
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(name)); |
|||
|
|||
while (control != null) |
|||
{ |
|||
var result = control.Styles.FindResource(name); |
|||
|
|||
if (result != PerspexProperty.UnsetValue) |
|||
{ |
|||
return result; |
|||
} |
|||
|
|||
control = control.StylingParent; |
|||
} |
|||
|
|||
return PerspexProperty.UnsetValue; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Generic; |
|||
using Perspex.Controls; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.Styling.UnitTests |
|||
{ |
|||
public class ResourceTests |
|||
{ |
|||
[Fact] |
|||
public void FindStyleResource_Should_Find_Correct_Resource() |
|||
{ |
|||
Border target; |
|||
|
|||
var tree = new Decorator |
|||
{ |
|||
Styles = new Styles |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = new Dictionary<string, object> |
|||
{ |
|||
{ "Foo", "foo resource" }, |
|||
{ "Bar", "overridden" }, |
|||
} |
|||
} |
|||
}, |
|||
Child = target = new Border |
|||
{ |
|||
Styles = new Styles |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = new Dictionary<string, object> |
|||
{ |
|||
{ "Bar", "again overridden" }, |
|||
} |
|||
}, |
|||
new Style |
|||
{ |
|||
Resources = new Dictionary<string, object> |
|||
{ |
|||
{ "Bar", "bar resource" }, |
|||
} |
|||
} |
|||
} |
|||
} |
|||
}; |
|||
|
|||
Assert.Equal("foo resource", target.FindStyleResource("Foo")); |
|||
Assert.Equal("bar resource", target.FindStyleResource("Bar")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void FindStyleResource_Should_Return_UnsetValue_For_Not_Found() |
|||
{ |
|||
Border target; |
|||
|
|||
var tree = target = new Border |
|||
{ |
|||
Styles = new Styles |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = new Dictionary<string, object> |
|||
{ |
|||
{ "Foo", "foo" }, |
|||
} |
|||
}, |
|||
} |
|||
}; |
|||
|
|||
Assert.Equal(PerspexProperty.UnsetValue, target.FindStyleResource("Baz")); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue