AdornerContainer.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Windows;
  2. using System.Windows.Documents;
  3. using System.Windows.Media;
  4. namespace HandyControl.Interactivity;
  5. public class AdornerContainer : Adorner
  6. {
  7. private UIElement _child;
  8. public AdornerContainer(UIElement adornedElement) : base(adornedElement)
  9. {
  10. }
  11. public UIElement Child
  12. {
  13. get => _child;
  14. set
  15. {
  16. if (value == null)
  17. {
  18. RemoveVisualChild(_child);
  19. // ReSharper disable once ExpressionIsAlwaysNull
  20. _child = value;
  21. return;
  22. }
  23. AddVisualChild(value);
  24. _child = value;
  25. }
  26. }
  27. protected override int VisualChildrenCount => _child != null ? 1 : 0;
  28. protected override Size ArrangeOverride(Size finalSize)
  29. {
  30. _child?.Arrange(new Rect(finalSize));
  31. return finalSize;
  32. }
  33. protected override Visual GetVisualChild(int index)
  34. {
  35. if (index == 0 && _child != null) return _child;
  36. return base.GetVisualChild(index);
  37. }
  38. }