BindingExtensionTests.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Styling;
  9. using Avalonia.UnitTests;
  10. using Xunit;
  11. namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
  12. {
  13. public class BindingExtensionTests : XamlTestBase
  14. {
  15. [Fact]
  16. public void BindingExtension_Binds_To_Source()
  17. {
  18. using (StyledWindow())
  19. {
  20. var xaml = @"
  21. <Window xmlns='https://github.com/avaloniaui'
  22. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  23. <Window.Resources>
  24. <x:String x:Key='text'>foobar</x:String>
  25. </Window.Resources>
  26. <TextBlock Name='textBlock' Text='{Binding Source={StaticResource text}}'/>
  27. </Window>";
  28. var loader = new AvaloniaXamlLoader();
  29. var window = (Window)loader.Load(xaml);
  30. var textBlock = window.FindControl<TextBlock>("textBlock");
  31. window.Show();
  32. Assert.Equal("foobar", textBlock.Text);
  33. }
  34. }
  35. private IDisposable StyledWindow(params (string, string)[] assets)
  36. {
  37. var services = TestServices.StyledWindow.With(
  38. assetLoader: new MockAssetLoader(assets),
  39. theme: () => new Styles
  40. {
  41. WindowStyle(),
  42. });
  43. return UnitTestApplication.Start(services);
  44. }
  45. private Style WindowStyle()
  46. {
  47. return new Style(x => x.OfType<Window>())
  48. {
  49. Setters =
  50. {
  51. new Setter(
  52. Window.TemplateProperty,
  53. new FuncControlTemplate<Window>((x, scope) =>
  54. new VisualLayerManager
  55. {
  56. Child =
  57. new ContentPresenter
  58. {
  59. Name = "PART_ContentPresenter",
  60. [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
  61. }.RegisterInNameScope(scope)
  62. }))
  63. }
  64. };
  65. }
  66. }
  67. }