BindingTests_Source.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Moq;
  2. using Avalonia.Controls;
  3. using Avalonia.Data;
  4. using Avalonia.Markup.Data;
  5. using Xunit;
  6. using System.ComponentModel;
  7. using System.Runtime.CompilerServices;
  8. using Avalonia.UnitTests;
  9. namespace Avalonia.Markup.UnitTests.Data
  10. {
  11. public class BindingTests_Source : ScopedTestBase
  12. {
  13. [Fact]
  14. public void Source_Should_Be_Used()
  15. {
  16. var source = new Source { Foo = "foo" };
  17. var binding = new Binding { Source = source, Path = "Foo" };
  18. var target = new TextBlock();
  19. target.Bind(TextBlock.TextProperty, binding);
  20. Assert.Equal("foo", target.Text);
  21. }
  22. public class Source : INotifyPropertyChanged
  23. {
  24. private string _foo;
  25. public string Foo
  26. {
  27. get => _foo;
  28. set
  29. {
  30. _foo = value;
  31. RaisePropertyChanged();
  32. }
  33. }
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. private void RaisePropertyChanged([CallerMemberName] string prop = "")
  36. {
  37. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
  38. }
  39. }
  40. }
  41. }