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

SilverLight

开发平台:

C#

  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace ESRI.ArcGIS.Samples
  4. {
  5. public class LayerCheckBox : CheckBox
  6. {
  7. public LayerCheckBox()
  8. {
  9. this.Checked += new RoutedEventHandler(LayerCheckBox_Checked);
  10. this.Unchecked += new RoutedEventHandler(LayerCheckBox_Unchecked);
  11. }
  12. void LayerCheckBox_Unchecked(object sender, RoutedEventArgs e)
  13. {
  14. if (Configurator != null && ConfiguratorParent != null)
  15. {
  16. ConfiguratorParent.Children.Remove(Configurator);
  17. }
  18. }
  19. void LayerCheckBox_Checked(object sender, RoutedEventArgs e)
  20. {
  21. //When a layer is checked on, add configurator to panel if it has one
  22. if (Configurator != null && ConfiguratorParent != null)
  23. {
  24. ConfiguratorParent.Children.Add(Configurator);
  25. }
  26. }
  27. /// <summary>
  28. /// Identifies the <see cref="Map"/> dependency property.
  29. /// </summary>
  30. public static readonly DependencyProperty MapProperty =
  31. DependencyProperty.Register("Map", typeof(ESRI.ArcGIS.Client.Map), typeof(LayerCheckBox),
  32. new PropertyMetadata(null, OnMapPropertyChanged));
  33. /// <summary>
  34. /// Gets or sets Map.
  35. /// </summary>
  36. public ESRI.ArcGIS.Client.Map Map
  37. {
  38. get { return (ESRI.ArcGIS.Client.Map)GetValue(MapProperty); }
  39. set { SetValue(MapProperty, value); }
  40. }
  41. /// <summary>
  42. /// MapProperty property changed handler. 
  43. /// </summary>
  44. /// <param name="d">LayerCheckBox that changed its Map.</param>
  45. /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
  46. private static void OnMapPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  47. {
  48. LayerCheckBox dp = d as LayerCheckBox;
  49. ESRI.ArcGIS.Client.Map newMap = (ESRI.ArcGIS.Client.Map)e.NewValue;
  50. ESRI.ArcGIS.Client.Map oldMap = (ESRI.ArcGIS.Client.Map)e.OldValue;
  51. if (oldMap != null && dp.Layer != null)
  52. {
  53. oldMap.Layers.Remove(dp.Layer);
  54. }
  55. if (newMap != null && dp.Layer != null && dp.IsChecked.Value)
  56. {
  57. newMap.Layers.Add(dp.Layer);
  58. }
  59. }
  60. protected override void OnToggle()
  61. {
  62. base.OnToggle();
  63. if (this.Layer!=null && this.Map!=null)
  64. {
  65. if (this.IsChecked.Value)
  66. {
  67. Map.Layers.Insert(Map.Layers.Count - 1, Layer);
  68. }
  69. else
  70. {
  71. Map.Layers.Remove(Layer);
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Identifies the <see cref="Layer"/> dependency property.
  77. /// </summary>
  78. public static readonly DependencyProperty LayerProperty =
  79. DependencyProperty.Register("Layer", typeof(ESRI.ArcGIS.Client.Layer), typeof(LayerCheckBox),
  80. new PropertyMetadata(null, OnLayerPropertyChanged));
  81. /// <summary>
  82. /// Gets or sets Layer.
  83. /// </summary>
  84. public ESRI.ArcGIS.Client.Layer Layer
  85. {
  86. get { return (ESRI.ArcGIS.Client.Layer)GetValue(LayerProperty); }
  87. set { SetValue(LayerProperty, value); }
  88. }
  89. /// <summary>
  90. /// LayerProperty property changed handler. 
  91. /// </summary>
  92. /// <param name="d">LayerCheckBox that changed its Layer.</param>
  93. /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
  94. private static void OnLayerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  95. {
  96. LayerCheckBox dp = d as LayerCheckBox;
  97. ESRI.ArcGIS.Client.Layer newLayer = (ESRI.ArcGIS.Client.Layer)e.NewValue;
  98. ESRI.ArcGIS.Client.Layer oldLayer = (ESRI.ArcGIS.Client.Layer)e.OldValue;
  99. if (dp.Map != null)
  100. {
  101. if (oldLayer != null)
  102. dp.Map.Layers.Remove(oldLayer);
  103. if (newLayer != null && dp.IsChecked.Value)
  104. dp.Map.Layers.Add(newLayer);
  105. }
  106. if (dp.Configurator != null)
  107. {
  108. dp.Configurator.Layer = newLayer;
  109. if (newLayer != null)
  110. dp.Configurator.Title = newLayer.ID;
  111. }
  112. }
  113. /// <summary>
  114. /// Identifies the <see cref="Configurator"/> dependency property.
  115. /// </summary>
  116. public static readonly DependencyProperty ConfiguratorProperty =
  117. DependencyProperty.Register("Configurator", typeof(ConfiguratorContainer), typeof(LayerCheckBox),
  118. new PropertyMetadata(null, OnConfiguratorPropertyChanged));
  119. /// <summary>
  120. /// Gets or sets Configurator.
  121. /// </summary>
  122. public ConfiguratorContainer Configurator
  123. {
  124. get { return (ConfiguratorContainer)GetValue(ConfiguratorProperty); }
  125. set { SetValue(ConfiguratorProperty, value); }
  126. }
  127. /// <summary>
  128. /// ConfiguratorProperty property changed handler. 
  129. /// </summary>
  130. /// <param name="d">LayerCheckBox that changed its Configurator.</param>
  131. /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
  132. private static void OnConfiguratorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  133. {
  134. LayerCheckBox dp = d as LayerCheckBox;
  135. ConfiguratorContainer newPanel = (ConfiguratorContainer)e.NewValue;
  136. ConfiguratorContainer oldPanel = (ConfiguratorContainer)e.OldValue;
  137. if (oldPanel != null) oldPanel.Layer = null;
  138. if (newPanel != null)
  139. {
  140. newPanel.Layer = dp.Layer;
  141. if (dp.Layer != null)
  142. newPanel.Title = dp.Layer.ID;
  143. }
  144. }
  145. public Panel ConfiguratorParent
  146. {
  147. get { return (Panel)GetValue(ConfiguratorParentProperty); }
  148. set { SetValue(ConfiguratorParentProperty, value); }
  149. }
  150. public static readonly DependencyProperty ConfiguratorParentProperty =
  151. DependencyProperty.Register("ConfiguratorParent", typeof(Panel), typeof(LayerCheckBox), null);
  152. }
  153. }