BindingExtensionTests.cs 2.1 KB

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