Browse Source

Prevent ControlTheme as a nested style.

pull/8262/head
Steven Kirk 4 years ago
parent
commit
a6dc6b1c88
  1. 16
      src/Avalonia.Base/Styling/ControlTheme.cs
  2. 28
      tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs

16
src/Avalonia.Base/Styling/ControlTheme.cs

@ -7,6 +7,17 @@ namespace Avalonia.Styling
/// </summary>
public class ControlTheme : StyleBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ControlTheme"/> class.
/// </summary>
public ControlTheme() { }
/// <summary>
/// Initializes a new instance of the <see cref="ControlTheme"/> class.
/// </summary>
/// <param name="targetType">The value for <see cref="TargetType"/>.</param>
public ControlTheme(Type targetType) => TargetType = targetType;
/// <summary>
/// Gets or sets the type for which this control theme is intended.
/// </summary>
@ -23,5 +34,10 @@ namespace Avalonia.Styling
SelectorMatch.AlwaysThisType :
SelectorMatch.NeverThisType;
}
internal override void SetParent(StyleBase? parent)
{
throw new InvalidOperationException("ControlThemes cannot be added as a nested style.");
}
}
}

28
tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs

@ -0,0 +1,28 @@
using System;
using Avalonia.Controls;
using Avalonia.Styling;
using Xunit;
namespace Avalonia.Base.UnitTests.Styling
{
public class ControlThemeTests
{
[Fact]
public void ControlTheme_Cannot_Be_Added_To_Style_Children()
{
var target = new ControlTheme(typeof(Button));
var style = new Style();
Assert.Throws<InvalidOperationException>(() => style.Children.Add(target));
}
[Fact]
public void ControlTheme_Cannot_Be_Added_To_ControlTheme_Children()
{
var target = new ControlTheme(typeof(Button));
var other = new ControlTheme(typeof(CheckBox));
Assert.Throws<InvalidOperationException>(() => other.Children.Add(target));
}
}
}
Loading…
Cancel
Save