By defining a StylingParent property and tying it to InheritanceParent. Fixes #312.
@@ -87,6 +87,10 @@
<Compile Include="Utilities\WeakSubscriptionManager.cs" />
</ItemGroup>
<ItemGroup>
+ <Reference Include="JetBrains.Annotations.PCL328, Version=9.2.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
+ <HintPath>..\..\packages\JetBrains.Annotations.9.2.0\lib\portable-net4+sl5+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\JetBrains.Annotations.PCL328.dll</HintPath>
+ <Private>True</Private>
+ </Reference>
<Reference Include="System.Reactive.Core">
<HintPath>..\..\packages\Rx-Core.2.2.5\lib\portable-windows8+net45+wp8\System.Reactive.Core.dll</HintPath>
</Reference>
@@ -218,6 +218,11 @@ namespace Perspex.Controls
/// </remarks>
Type IStyleable.StyleKey => GetType();
+ /// <summary>
+ /// Gets the parent style host element.
+ /// </summary>
+ IStyleHost IStyleHost.StylingParent => (IStyleHost)InheritanceParent;
+
/// <summary>
/// Gets a value which indicates whether a change to the <see cref="DataContext"/> is in
/// the process of being notified.
@@ -3,8 +3,20 @@
namespace Perspex.Styling
{
+ /// Defines an element that has a <see cref="Styles"/> collection.
public interface IStyleHost : IVisual
+ /// Gets the styles for the element.
Styles Styles { get; }
+ IStyleHost StylingParent { get; }
}
@@ -31,11 +31,7 @@ namespace Perspex.Styling
Contract.Requires<ArgumentNullException>(control != null);
Contract.Requires<ArgumentNullException>(container != null);
- IVisual visual = container as IVisual;
-
- IStyleHost parentContainer = visual?.GetVisualAncestors()
- .OfType<IStyleHost>()
- .FirstOrDefault();
+ var parentContainer = container.StylingParent;
if (parentContainer != null)