StyleTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Xml;
  7. using Avalonia.Controls;
  8. using Avalonia.Markup.Xaml.Styling;
  9. using Avalonia.Markup.Xaml.Templates;
  10. using Avalonia.Media;
  11. using Avalonia.Styling;
  12. using Avalonia.UnitTests;
  13. using Xunit;
  14. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  15. {
  16. public class StyleTests : XamlTestBase
  17. {
  18. static StyleTests()
  19. {
  20. GC.KeepAlive(typeof(ItemsRepeater));
  21. }
  22. [Fact]
  23. public void Color_Can_Be_Added_To_Style_Resources()
  24. {
  25. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  26. {
  27. var xaml = @"
  28. <UserControl xmlns='https://github.com/avaloniaui'
  29. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  30. <UserControl.Styles>
  31. <Style>
  32. <Style.Resources>
  33. <Color x:Key='color'>#ff506070</Color>
  34. </Style.Resources>
  35. </Style>
  36. </UserControl.Styles>
  37. </UserControl>";
  38. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  39. var color = (Color)((Style)userControl.Styles[0]).Resources["color"];
  40. Assert.Equal(0xff506070, color.ToUint32());
  41. }
  42. }
  43. [Fact]
  44. public void DataTemplate_Can_Be_Added_To_Style_Resources()
  45. {
  46. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  47. {
  48. var xaml = @"
  49. <UserControl xmlns='https://github.com/avaloniaui'
  50. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  51. <UserControl.Styles>
  52. <Style>
  53. <Style.Resources>
  54. <DataTemplate x:Key='dataTemplate'><TextBlock/></DataTemplate>
  55. </Style.Resources>
  56. </Style>
  57. </UserControl.Styles>
  58. </UserControl>";
  59. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  60. var dataTemplate = (DataTemplate)((Style)userControl.Styles[0]).Resources["dataTemplate"];
  61. Assert.NotNull(dataTemplate);
  62. }
  63. }
  64. [Fact]
  65. public void ControlTemplate_Can_Be_Added_To_Style_Resources()
  66. {
  67. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  68. {
  69. var xaml = @"
  70. <UserControl xmlns='https://github.com/avaloniaui'
  71. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  72. <UserControl.Styles>
  73. <Style>
  74. <Style.Resources>
  75. <ControlTemplate x:Key='controlTemplate' TargetType='{x:Type Button}'>
  76. <ContentPresenter Content='{TemplateBinding Content}'/>
  77. </ControlTemplate>
  78. </Style.Resources>
  79. </Style>
  80. </UserControl.Styles>
  81. </UserControl>";
  82. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  83. var controlTemplate = (ControlTemplate)((Style)userControl.Styles[0]).Resources["controlTemplate"];
  84. Assert.NotNull(controlTemplate);
  85. Assert.Equal(typeof(Button), controlTemplate.TargetType);
  86. }
  87. }
  88. [Fact]
  89. public void SolidColorBrush_Can_Be_Added_To_Style_Resources()
  90. {
  91. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  92. {
  93. var xaml = @"
  94. <UserControl xmlns='https://github.com/avaloniaui'
  95. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  96. <UserControl.Styles>
  97. <Style>
  98. <Style.Resources>
  99. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  100. </Style.Resources>
  101. </Style>
  102. </UserControl.Styles>
  103. </UserControl>";
  104. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  105. var brush = (ISolidColorBrush)((Style)userControl.Styles[0]).Resources["brush"];
  106. Assert.Equal(0xff506070, brush.Color.ToUint32());
  107. }
  108. }
  109. [Fact]
  110. public void Setter_Can_Contain_Template()
  111. {
  112. using (UnitTestApplication.Start(TestServices.StyledWindow))
  113. {
  114. var xaml = @"
  115. <Window xmlns='https://github.com/avaloniaui'
  116. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  117. <Window.Styles>
  118. <Style Selector='ContentControl'>
  119. <Setter Property='Content'>
  120. <Template>
  121. <TextBlock>Hello World!</TextBlock>
  122. </Template>
  123. </Setter>
  124. </Style>
  125. </Window.Styles>
  126. <ContentControl Name='target'/>
  127. </Window>";
  128. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  129. var target = window.Find<ContentControl>("target");
  130. Assert.IsType<TextBlock>(target.Content);
  131. Assert.Equal("Hello World!", ((TextBlock)target.Content).Text);
  132. }
  133. }
  134. [Fact]
  135. public void Setter_Value_Is_Bound_Directly_If_The_Target_Type_Derives_From_ITemplate()
  136. {
  137. using (UnitTestApplication.Start(TestServices.StyledWindow))
  138. {
  139. var xaml = @"
  140. <Window xmlns='https://github.com/avaloniaui'
  141. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  142. <Window.Styles>
  143. <Style Selector=':is(Control)'>
  144. <Setter Property='FocusAdorner'>
  145. <FocusAdornerTemplate>
  146. <Rectangle Stroke='Black'
  147. StrokeThickness='1'
  148. StrokeDashArray='1,2'/>
  149. </FocusAdornerTemplate>
  150. </Setter>
  151. </Style>
  152. </Window.Styles>
  153. <TextBlock Name='target'/>
  154. </Window>";
  155. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  156. var target = window.Find<TextBlock>("target");
  157. Assert.NotNull(target.FocusAdorner);
  158. }
  159. }
  160. [Fact]
  161. public void Setter_Can_Set_Attached_Property()
  162. {
  163. using (UnitTestApplication.Start(TestServices.StyledWindow))
  164. {
  165. var xaml = @"
  166. <Window xmlns='https://github.com/avaloniaui'
  167. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  168. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  169. <Window.Styles>
  170. <Style Selector='TextBlock'>
  171. <Setter Property='DockPanel.Dock' Value='Right'/>
  172. </Style>
  173. </Window.Styles>
  174. <TextBlock/>
  175. </Window>";
  176. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  177. var textBlock = (TextBlock)window.Content;
  178. window.ApplyTemplate();
  179. Assert.Equal(Dock.Right, DockPanel.GetDock(textBlock));
  180. }
  181. }
  182. [Fact(Skip = "The animation system currently needs to be able to set any property on any object")]
  183. public void Disallows_Setting_Non_Registered_Property()
  184. {
  185. using (UnitTestApplication.Start(TestServices.StyledWindow))
  186. {
  187. var xaml = @"
  188. <Window xmlns='https://github.com/avaloniaui'
  189. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  190. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  191. <Window.Styles>
  192. <Style Selector='TextBlock'>
  193. <Setter Property='Button.IsDefault' Value='True'/>
  194. </Style>
  195. </Window.Styles>
  196. <TextBlock/>
  197. </Window>";
  198. var ex = Assert.Throws<XmlException>(() => AvaloniaRuntimeXamlLoader.Load(xaml));
  199. Assert.Equal(
  200. "Property 'Button.IsDefault' is not registered on 'Avalonia.Controls.TextBlock'.",
  201. ex.InnerException.Message);
  202. }
  203. }
  204. [Fact]
  205. public void Style_Can_Use_Not_Selector()
  206. {
  207. using (UnitTestApplication.Start(TestServices.StyledWindow))
  208. {
  209. var xaml = @"
  210. <Window xmlns='https://github.com/avaloniaui'
  211. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  212. <Window.Styles>
  213. <Style Selector='Border:not(.foo)'>
  214. <Setter Property='Background' Value='Red'/>
  215. </Style>
  216. </Window.Styles>
  217. <StackPanel>
  218. <Border Name='foo' Classes='foo bar'/>
  219. <Border Name='notFoo' Classes='bar'/>
  220. </StackPanel>
  221. </Window>";
  222. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  223. var foo = window.FindControl<Border>("foo");
  224. var notFoo = window.FindControl<Border>("notFoo");
  225. Assert.Null(foo.Background);
  226. Assert.Equal(Colors.Red, ((ISolidColorBrush)notFoo.Background).Color);
  227. }
  228. }
  229. [Fact]
  230. public void Style_Can_Use_NthChild_Selector()
  231. {
  232. using (UnitTestApplication.Start(TestServices.StyledWindow))
  233. {
  234. var xaml = @"
  235. <Window xmlns='https://github.com/avaloniaui'
  236. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  237. <Window.Styles>
  238. <Style Selector='Border.foo:nth-child(2n+1)'>
  239. <Setter Property='Background' Value='Red'/>
  240. </Style>
  241. </Window.Styles>
  242. <StackPanel>
  243. <Border x:Name='b1' Classes='foo'/>
  244. <Border x:Name='b2' />
  245. </StackPanel>
  246. </Window>";
  247. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  248. var b1 = window.FindControl<Border>("b1");
  249. var b2 = window.FindControl<Border>("b2");
  250. Assert.Equal(Brushes.Red, b1.Background);
  251. Assert.Null(b2.Background);
  252. }
  253. }
  254. [Fact]
  255. public void Style_Can_Use_NthChild_Selector_After_Reorder()
  256. {
  257. using (UnitTestApplication.Start(TestServices.StyledWindow))
  258. {
  259. var xaml = @"
  260. <Window xmlns='https://github.com/avaloniaui'
  261. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  262. <Window.Styles>
  263. <Style Selector='Border:nth-child(2n)'>
  264. <Setter Property='Background' Value='Red'/>
  265. </Style>
  266. </Window.Styles>
  267. <StackPanel x:Name='parent'>
  268. <Border x:Name='b1' />
  269. <Border x:Name='b2' />
  270. </StackPanel>
  271. </Window>";
  272. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  273. var parent = window.FindControl<StackPanel>("parent");
  274. var b1 = window.FindControl<Border>("b1");
  275. var b2 = window.FindControl<Border>("b2");
  276. Assert.Null(b1.Background);
  277. Assert.Equal(Brushes.Red, b2.Background);
  278. parent.Children.Remove(b1);
  279. Assert.Null(b1.Background);
  280. Assert.Null(b2.Background);
  281. parent.Children.Add(b1);
  282. Assert.Equal(Brushes.Red, b1.Background);
  283. Assert.Null(b2.Background);
  284. }
  285. }
  286. [Fact]
  287. public void Style_Can_Use_NthLastChild_Selector_After_Reorder()
  288. {
  289. using (UnitTestApplication.Start(TestServices.StyledWindow))
  290. {
  291. var xaml = @"
  292. <Window xmlns='https://github.com/avaloniaui'
  293. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  294. <Window.Styles>
  295. <Style Selector='Border:nth-last-child(2n)'>
  296. <Setter Property='Background' Value='Red'/>
  297. </Style>
  298. </Window.Styles>
  299. <StackPanel x:Name='parent'>
  300. <Border x:Name='b1' />
  301. <Border x:Name='b2' />
  302. </StackPanel>
  303. </Window>";
  304. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  305. var parent = window.FindControl<StackPanel>("parent");
  306. var b1 = window.FindControl<Border>("b1");
  307. var b2 = window.FindControl<Border>("b2");
  308. Assert.Equal(Brushes.Red, b1.Background);
  309. Assert.Null(b2.Background);
  310. parent.Children.Remove(b1);
  311. Assert.Null(b1.Background);
  312. Assert.Null(b2.Background);
  313. parent.Children.Add(b1);
  314. Assert.Null(b1.Background);
  315. Assert.Equal(Brushes.Red, b2.Background);
  316. }
  317. }
  318. [Fact]
  319. public void Style_Can_Use_NthChild_Selector_With_ListBox()
  320. {
  321. using (UnitTestApplication.Start(TestServices.StyledWindow))
  322. {
  323. var xaml = @"
  324. <Window xmlns='https://github.com/avaloniaui'
  325. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  326. <Window.Styles>
  327. <Style Selector='ListBoxItem:nth-child(2n)'>
  328. <Setter Property='Background' Value='{Binding}'/>
  329. </Style>
  330. </Window.Styles>
  331. <ListBox x:Name='list' />
  332. </Window>";
  333. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  334. var collection = new ObservableCollection<IBrush>()
  335. {
  336. Brushes.Red, Brushes.Green, Brushes.Blue
  337. };
  338. var list = window.FindControl<ListBox>("list");
  339. list.Items = collection;
  340. window.Show();
  341. IEnumerable<IBrush> GetColors() => list.GetRealizedContainers().Cast<ListBoxItem>().Select(t => t.Background);
  342. Assert.Equal(new[] { Brushes.Transparent, Brushes.Green, Brushes.Transparent }, GetColors());
  343. collection.Remove(Brushes.Green);
  344. window.LayoutManager.ExecuteLayoutPass();
  345. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue }, GetColors());
  346. collection.Add(Brushes.Violet);
  347. collection.Add(Brushes.Black);
  348. window.LayoutManager.ExecuteLayoutPass();
  349. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue, Brushes.Transparent, Brushes.Black }, GetColors());
  350. }
  351. }
  352. [Fact]
  353. public void Style_Can_Use_NthChild_Selector_With_ItemsRepeater()
  354. {
  355. using (UnitTestApplication.Start(TestServices.StyledWindow))
  356. {
  357. var xaml = @"
  358. <Window xmlns='https://github.com/avaloniaui'
  359. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  360. <Window.Styles>
  361. <Style Selector='TextBlock'>
  362. <Setter Property='Foreground' Value='Transparent'/>
  363. </Style>
  364. <Style Selector='TextBlock:nth-child(2n)'>
  365. <Setter Property='Foreground' Value='{Binding}'/>
  366. </Style>
  367. </Window.Styles>
  368. <ItemsRepeater x:Name='list' />
  369. </Window>";
  370. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  371. var collection = new ObservableCollection<IBrush>()
  372. {
  373. Brushes.Red, Brushes.Green, Brushes.Blue
  374. };
  375. var list = window.FindControl<ItemsRepeater>("list");
  376. list.Items = collection;
  377. window.Show();
  378. IEnumerable<IBrush> GetColors() => Enumerable.Range(0, list.ItemsSourceView.Count)
  379. .Select(t => (list.GetOrCreateElement(t) as TextBlock)!.Foreground);
  380. Assert.Equal(new[] { Brushes.Transparent, Brushes.Green, Brushes.Transparent }, GetColors());
  381. collection.Remove(Brushes.Green);
  382. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue }, GetColors().ToList());
  383. collection.Add(Brushes.Violet);
  384. collection.Add(Brushes.Black);
  385. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue, Brushes.Transparent, Brushes.Black }, GetColors());
  386. }
  387. }
  388. [Fact]
  389. public void Style_Can_Use_Or_Selector_1()
  390. {
  391. using (UnitTestApplication.Start(TestServices.StyledWindow))
  392. {
  393. var xaml = @"
  394. <Window xmlns='https://github.com/avaloniaui'
  395. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  396. <Window.Styles>
  397. <Style Selector='Border.foo, Border.bar'>
  398. <Setter Property='Background' Value='Red'/>
  399. </Style>
  400. </Window.Styles>
  401. <StackPanel>
  402. <Border Name='foo' Classes='foo'/>
  403. <Border Name='bar' Classes='bar'/>
  404. <Border Name='baz' Classes='baz'/>
  405. </StackPanel>
  406. </Window>";
  407. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  408. var foo = window.FindControl<Border>("foo");
  409. var bar = window.FindControl<Border>("bar");
  410. var baz = window.FindControl<Border>("baz");
  411. Assert.Equal(Brushes.Red, foo.Background);
  412. Assert.Equal(Brushes.Red, bar.Background);
  413. Assert.Null(baz.Background);
  414. }
  415. }
  416. [Fact]
  417. public void Style_Can_Use_Or_Selector_2()
  418. {
  419. using (UnitTestApplication.Start(TestServices.StyledWindow))
  420. {
  421. var xaml = @"
  422. <Window xmlns='https://github.com/avaloniaui'
  423. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  424. <Window.Styles>
  425. <Style Selector='Button,Carousel,ListBox'>
  426. <Setter Property='Background' Value='Red'/>
  427. </Style>
  428. </Window.Styles>
  429. <StackPanel>
  430. <Button Name='button'/>
  431. <Carousel Name='carousel'/>
  432. <ListBox Name='listBox'/>
  433. </StackPanel>
  434. </Window>";
  435. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  436. var button = window.FindControl<Button>("button");
  437. var carousel = window.FindControl<Carousel>("carousel");
  438. var listBox = window.FindControl<ListBox>("listBox");
  439. Assert.Equal(Brushes.Red, button.Background);
  440. Assert.Equal(Brushes.Red, carousel.Background);
  441. Assert.Equal(Brushes.Red, listBox.Background);
  442. }
  443. }
  444. [Fact]
  445. public void Transitions_Can_Be_Styled()
  446. {
  447. using (UnitTestApplication.Start(TestServices.StyledWindow))
  448. {
  449. var xaml = @"
  450. <Window xmlns='https://github.com/avaloniaui'
  451. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  452. <Window.Styles>
  453. <Style Selector='Border'>
  454. <Setter Property='Transitions'>
  455. <Transitions>
  456. <DoubleTransition Property='Width' Duration='0:0:1'/>
  457. </Transitions>
  458. </Setter>
  459. </Style>
  460. <Style Selector='Border.foo'>
  461. <Setter Property='Transitions'>
  462. <Transitions>
  463. <DoubleTransition Property='Height' Duration='0:0:1'/>
  464. </Transitions>
  465. </Setter>
  466. </Style>
  467. </Window.Styles>
  468. <Border/>
  469. </Window>";
  470. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  471. var border = (Border)window.Content;
  472. Assert.Equal(1, border.Transitions.Count);
  473. Assert.Equal(Border.WidthProperty, border.Transitions[0].Property);
  474. border.Classes.Add("foo");
  475. Assert.Equal(1, border.Transitions.Count);
  476. Assert.Equal(Border.HeightProperty, border.Transitions[0].Property);
  477. border.Classes.Remove("foo");
  478. Assert.Equal(1, border.Transitions.Count);
  479. Assert.Equal(Border.WidthProperty, border.Transitions[0].Property);
  480. }
  481. }
  482. [Fact]
  483. public void Style_Can_Use_Class_Selector_With_Dash()
  484. {
  485. using (UnitTestApplication.Start(TestServices.StyledWindow))
  486. {
  487. var xaml = @"
  488. <Window xmlns='https://github.com/avaloniaui'
  489. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  490. <Window.Styles>
  491. <Style Selector='Border.foo-bar'>
  492. <Setter Property='Background' Value='Red'/>
  493. </Style>
  494. </Window.Styles>
  495. <StackPanel>
  496. <Border Name='foo' Classes='foo-bar'/>
  497. </StackPanel>
  498. </Window>";
  499. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  500. var foo = window.FindControl<Border>("foo");
  501. Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color);
  502. }
  503. }
  504. [Fact]
  505. public void Style_Can_Use_Pseudolass_Selector_With_Dash()
  506. {
  507. using (UnitTestApplication.Start(TestServices.StyledWindow))
  508. {
  509. var xaml = @"
  510. <Window xmlns='https://github.com/avaloniaui'
  511. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  512. <Window.Styles>
  513. <Style Selector='Border:foo-bar'>
  514. <Setter Property='Background' Value='Red'/>
  515. </Style>
  516. </Window.Styles>
  517. <StackPanel>
  518. <Border Name='foo'/>
  519. </StackPanel>
  520. </Window>";
  521. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  522. var foo = window.FindControl<Border>("foo");
  523. Assert.Null(foo.Background);
  524. ((IPseudoClasses)foo.Classes).Add(":foo-bar");
  525. Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color);
  526. }
  527. }
  528. [Fact]
  529. public void Can_Use_Nested_Styles()
  530. {
  531. using (UnitTestApplication.Start(TestServices.StyledWindow))
  532. {
  533. var xaml = @"
  534. <Window xmlns='https://github.com/avaloniaui'
  535. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  536. <Window.Styles>
  537. <Style Selector='Border'>
  538. <Style Selector='^.foo'>
  539. <Setter Property='Background' Value='Red'/>
  540. </Style>
  541. </Style>
  542. </Window.Styles>
  543. <StackPanel>
  544. <Border Name='foo'/>
  545. </StackPanel>
  546. </Window>";
  547. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  548. var foo = window.FindControl<Border>("foo");
  549. Assert.Null(foo.Background);
  550. foo.Classes.Add("foo");
  551. Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color);
  552. }
  553. }
  554. }
  555. }