StyleTests.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Linq;
  2. using System.Reactive.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Data;
  5. using Avalonia.PropertyStore;
  6. using Avalonia.Styling;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. namespace Avalonia.Markup.Xaml.UnitTests
  10. {
  11. public class StyleTests : XamlTestBase
  12. {
  13. [Fact]
  14. public void Binding_Should_Be_Assigned_To_Setter_Value_Instead_Of_Bound()
  15. {
  16. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  17. {
  18. var xaml = "<Style Selector='Button' xmlns='https://github.com/avaloniaui'><Setter Property='Content' Value='{Binding}'/></Style>";
  19. var style = (Style)AvaloniaRuntimeXamlLoader.Load(xaml);
  20. var setter = (Setter)(style.Setters.First());
  21. Assert.IsType<Binding>(setter.Value);
  22. }
  23. }
  24. [Fact]
  25. public void Setter_With_TwoWay_Binding_Should_Update_Source()
  26. {
  27. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  28. {
  29. var data = new Data
  30. {
  31. Foo = "foo",
  32. };
  33. var control = new TextBox
  34. {
  35. DataContext = data,
  36. };
  37. var style = new Style()
  38. {
  39. Setters =
  40. {
  41. new Setter
  42. {
  43. Property = TextBox.TextProperty,
  44. Value = new Binding
  45. {
  46. Path = "Foo",
  47. Mode = BindingMode.TwoWay
  48. }
  49. }
  50. }
  51. };
  52. StyleHelpers.TryAttach(style, control);
  53. Assert.Equal("foo", control.Text);
  54. control.Text = "bar";
  55. Assert.Equal("bar", data.Foo);
  56. }
  57. }
  58. private class Data
  59. {
  60. public string Foo { get; set; }
  61. }
  62. }
  63. }