FrameworkElementExtension.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Windows;
  2. namespace HandyControl.Tools.Extension;
  3. public static class FrameworkElementExtension
  4. {
  5. public static double GetValidWidth(this FrameworkElement element)
  6. {
  7. if (!double.IsNaN(element.Width))
  8. {
  9. if (element.Width > 0)
  10. {
  11. return element.Width;
  12. }
  13. }
  14. else
  15. {
  16. if (element.ActualWidth > 0)
  17. {
  18. return element.ActualWidth;
  19. }
  20. if (element.DesiredSize.Width > 0)
  21. {
  22. return element.DesiredSize.Width;
  23. }
  24. }
  25. return 0;
  26. }
  27. public static double GetValidHeight(this FrameworkElement element)
  28. {
  29. if (!double.IsNaN(element.Height))
  30. {
  31. if (element.Height > 0)
  32. {
  33. return element.Height;
  34. }
  35. }
  36. else
  37. {
  38. if (element.ActualHeight > 0)
  39. {
  40. return element.ActualHeight;
  41. }
  42. if (element.DesiredSize.Height > 0)
  43. {
  44. return element.DesiredSize.Height;
  45. }
  46. }
  47. return 0;
  48. }
  49. }