RotatingTextSymbol.cs
上传用户:huazai0421
上传日期:2008-05-30
资源大小:405k
文件大小:4k
源码类别:

SilverLight

开发平台:

C#

  1. using System;
  2. using System.Reflection;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using ESRI.ArcGIS.Client.Symbols;
  7. namespace ESRI.ArcGIS.Samples
  8. {
  9. public class RotatingTextSymbol : MarkerSymbol
  10. {
  11. private const string template = @"<ControlTemplate
  12. xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
  13. xmlns:local=""clr-namespace:{0};assembly={1}"">
  14. <Grid RenderTransformOrigin=""0.5,0.5"" Width=""100""
  15.   local:RotatingTextSymbol.AngleBinder=""{{Binding Path=Symbol.Angle}}"">
  16. <TextBlock FontWeight=""Bold"" Foreground=""White"" HorizontalAlignment=""Center"" VerticalAlignment=""Center""
  17. Text=""{{Binding Symbol.Text}}"" >
  18. <TextBlock.Effect><BlurEffect Radius=""5"" /></TextBlock.Effect>
  19. </TextBlock>
  20. <TextBlock FontWeight=""Bold"" HorizontalAlignment=""Center"" VerticalAlignment=""Center""
  21. Text=""{{Binding Symbol.Text}}"" />
  22. </Grid>
  23. </ControlTemplate>";
  24. public RotatingTextSymbol()
  25. {
  26. Type t = typeof(RotatingTextSymbol);
  27. Assembly asm = Assembly.GetCallingAssembly();
  28. string temp = string.Format(template, t.Namespace, t.Assembly.FullName.Split(new string[] { ", "}, StringSplitOptions.RemoveEmptyEntries)[0]);
  29. ControlTemplate = System.Windows.Markup.XamlReader.Load(temp) as ControlTemplate;
  30. this.OffsetX = 50;
  31. }
  32. /// <summary>
  33. /// Identifies the <see cref="Angle"/> dependency property.
  34. /// </summary>
  35. public static readonly DependencyProperty AngleProperty =
  36. DependencyProperty.Register("Angle", typeof(double), typeof(RotatingTextSymbol),
  37. new PropertyMetadata(0.0, OnAnglePropertyChanged));
  38. /// <summary>
  39. /// Gets or sets Angle.
  40. /// </summary>
  41. public double Angle
  42. {
  43. get { return (double)GetValue(AngleProperty); }
  44. set { SetValue(AngleProperty, value); }
  45. }
  46. /// <summary>
  47. /// AngleProperty property changed handler. 
  48. /// </summary>
  49. /// <param name="d">ownerclass that changed its Angle.</param>
  50. /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
  51. private static void OnAnglePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  52. {
  53. RotatingTextSymbol dp = d as RotatingTextSymbol;
  54. dp.OnPropertyChanged("Angle");
  55. }
  56. /// <summary>
  57. /// Identifies the <see cref="Text"/> dependency property.
  58. /// </summary>
  59. public static readonly DependencyProperty TextProperty =
  60. DependencyProperty.Register("Text", typeof(string), typeof(RotatingTextSymbol),
  61. new PropertyMetadata("", OnTextPropertyChanged));
  62. /// <summary>
  63. /// Gets or sets Text.
  64. /// </summary>
  65. public string Text
  66. {
  67. get { return (string)GetValue(TextProperty); }
  68. set { SetValue(TextProperty, value); }
  69. }
  70. /// <summary>
  71. /// AngleProperty property changed handler. 
  72. /// </summary>
  73. /// <param name="d">ownerclass that changed its Angle.</param>
  74. /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
  75. private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  76. {
  77. RotatingTextSymbol dp = d as RotatingTextSymbol;
  78. dp.OnPropertyChanged("Text");
  79. }
  80. public static readonly DependencyProperty AngleBinderProperty =
  81. DependencyProperty.RegisterAttached("AngleBinder", typeof(double),
  82. typeof(RotatingTextSymbol),
  83. new PropertyMetadata(0.0, OnAngleBinderChanged));
  84. public static double GetAngleBinder(DependencyObject d)
  85. {
  86. return (double)d.GetValue(AngleBinderProperty);
  87. }
  88. public static void SetAngleBinder(DependencyObject d, double value)
  89. {
  90. d.SetValue(AngleBinderProperty, value);
  91. }
  92. private static void OnAngleBinderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  93. {
  94. if (d is UIElement)
  95. {
  96. UIElement b = d as UIElement;
  97. if (e.NewValue is double)
  98. {
  99. double c = (double)e.NewValue;
  100. if (!double.IsNaN(c))
  101. {
  102. if (b.RenderTransform is RotateTransform)
  103. (b.RenderTransform as RotateTransform).Angle = c;
  104. else
  105. b.RenderTransform = new RotateTransform() { Angle = c };
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }