BindingTests_Source.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Moq;
  4. using Avalonia.Controls;
  5. using Avalonia.Data;
  6. using Avalonia.Markup.Data;
  7. using Avalonia.Markup.Xaml.Data;
  8. using Xunit;
  9. using System.ComponentModel;
  10. using System.Runtime.CompilerServices;
  11. namespace Avalonia.Markup.Xaml.UnitTests.Data
  12. {
  13. public class BindingTests_Source
  14. {
  15. [Fact]
  16. public void Source_Should_Be_Used()
  17. {
  18. var source = new Source { Foo = "foo" };
  19. var binding = new Binding { Source = source, Path = "Foo" };
  20. var target = new TextBlock();
  21. target.Bind(TextBlock.TextProperty, binding);
  22. Assert.Equal(target.Text, "foo");
  23. }
  24. public class Source : INotifyPropertyChanged
  25. {
  26. private string _foo;
  27. public string Foo
  28. {
  29. get { return _foo; }
  30. set
  31. {
  32. _foo = Foo;
  33. RaisePropertyChanged();
  34. }
  35. }
  36. public event PropertyChangedEventHandler PropertyChanged;
  37. private void RaisePropertyChanged([CallerMemberName] string prop = "")
  38. {
  39. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
  40. }
  41. }
  42. }
  43. }