CenterBorderGapMaskConverter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Media;
  10. using System.Windows.Shapes;
  11. using Brushes = System.Windows.Media.Brushes;
  12. namespace Aitex.Core.UI.Converters
  13. {
  14. class CenterBorderGapMaskConverter : IMultiValueConverter
  15. {
  16. // Methods
  17. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. Type type = typeof(double);
  20. if (values == null
  21. || values.Length != 3
  22. || values[0] == null
  23. || values[1] == null
  24. || values[2] == null
  25. || !type.IsAssignableFrom(values[0].GetType())
  26. || !type.IsAssignableFrom(values[1].GetType())
  27. || !type.IsAssignableFrom(values[2].GetType()))
  28. {
  29. return DependencyProperty.UnsetValue;
  30. }
  31. double pixels = (double)values[0];
  32. double width = (double)values[1];
  33. double height = (double)values[2];
  34. if ((width == 0.0) || (height == 0.0))
  35. {
  36. return null;
  37. }
  38. Grid visual = new Grid();
  39. visual.Width = width;
  40. visual.Height = height;
  41. ColumnDefinition colDefinition1 = new ColumnDefinition();
  42. ColumnDefinition colDefinition2 = new ColumnDefinition();
  43. ColumnDefinition colDefinition3 = new ColumnDefinition();
  44. colDefinition1.Width = new GridLength(1.0, GridUnitType.Star);
  45. colDefinition2.Width = new GridLength(pixels);
  46. colDefinition3.Width = new GridLength(1.0, GridUnitType.Star);
  47. visual.ColumnDefinitions.Add(colDefinition1);
  48. visual.ColumnDefinitions.Add(colDefinition2);
  49. visual.ColumnDefinitions.Add(colDefinition3);
  50. RowDefinition rowDefinition1 = new RowDefinition();
  51. RowDefinition rowDefinition2 = new RowDefinition();
  52. rowDefinition1.Height = new GridLength(height / 2.0);
  53. rowDefinition2.Height = new GridLength(1.0, GridUnitType.Star);
  54. visual.RowDefinitions.Add(rowDefinition1);
  55. visual.RowDefinitions.Add(rowDefinition2);
  56. Rectangle rectangle1 = new Rectangle();
  57. Rectangle rectangle2 = new Rectangle();
  58. Rectangle rectangle3 = new Rectangle();
  59. rectangle1.Fill = Brushes.Black;
  60. rectangle2.Fill = Brushes.Black;
  61. rectangle3.Fill = Brushes.Black;
  62. Grid.SetRowSpan(rectangle1, 2);
  63. Grid.SetRow(rectangle1, 0);
  64. Grid.SetColumn(rectangle1, 0);
  65. Grid.SetRow(rectangle2, 1);
  66. Grid.SetColumn(rectangle2, 1);
  67. Grid.SetRowSpan(rectangle3, 2);
  68. Grid.SetRow(rectangle3, 0);
  69. Grid.SetColumn(rectangle3, 2);
  70. visual.Children.Add(rectangle1);
  71. visual.Children.Add(rectangle2);
  72. visual.Children.Add(rectangle3);
  73. return new VisualBrush(visual);
  74. }
  75. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  76. {
  77. return new object[] { Binding.DoNothing };
  78. }
  79. }
  80. }