1、RadTreeView是Telerik公司提供的控件,与之对应的也有SL平台下的控件,使用方法跟Silverlight ToolKit中的TreeView中使用的HierarchicalDataTemplate类似,可以实现多级递归。
2、添加命名空间引用
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
3、添加资源引用
<UserControl.Resources>
<DataTemplate x:Key="V_Site">
<TextBlock Text="{Binding S_Name}" />
</DataTemplate>
<!--ItemTemplate="{StaticResource Team}"添加的是上面的id-->
<telerik2:HierarchicalDataTemplate x:Key="V_Region" ItemTemplate="{StaticResource V_Site}"
ItemsSource="{Binding V_Sites}">
<TextBlock Text="{Binding R_Name}" />
</telerik2:HierarchicalDataTemplate>
</UserControl.Resources>
3、控件添加
<telerik:RadTreeView x:Name="RadTreeView1"
SelectionMode="Extended" IsLineEnabled="True" IsExpandOnSingleClickEnabled="True"
ItemsOptionListType="None" IsOptionElementsEnabled="True"
IsRootLinesEnabled="True" IsTriStateMode="True" Selected="RadTreeView1_Selected" FontSize="14.667" Foreground="#FF0A539D" FontFamily="Microsoft YaHei" Canvas.ZIndex="1"
ItemsSource="{Binding SiteTypesDataSource}"
ItemTemplate="{StaticResource V_Region}" Grid.Row="2" Margin="33,6,25,5" />
4、几个类 Item_Collection_DistinctBy_VID.cs、V_Site.cs、V_Region.cs,分别如下:
public class Item_Collection_DistinctBy_VID : IEqualityComparer<Video>
{
public bool Equals(Video p1, Video p2)
{
if (p1 == null)
return p2 == null;
return p1.REGIONNM == p2.REGIONNM;
}
public int GetHashCode(Video p)
{
if (p == null)
return 0;
return p.REGIONNM.GetHashCode();
}
}
public class V_Site
{
public V_Site(string name, string code, double lgtd, double lttd)
{
this.S_Name = name;
this.S_Code = code;
this.S_LGTD = lgtd;
this.S_LTTD = lttd;
}
public string S_Code { get; set; } //站点编码
public string S_Name { get; set; } //站点名称
public double S_LGTD { get; set; } //经度
public double S_LTTD { get; set; } //纬度
}
public class V_Region
{
public V_Region(string name, string code)
{
this.R_Name = name;
this.R_Code = code;
this.V_Sites = new ObservableCollection<V_Site>();
}
public string R_Name { get; set; } //地区名称
public string R_Code { get; set; } //地区编码
public ObservableCollection<V_Site> V_Sites { get; set; } //站点类型
}
5、数据绑定
#region 树状绑定事件,使用LINQ来处理一次性获取的数据,然后绑定到RadTreeView中
public ObservableCollection<V_Region> SiteTypesDataSource { get; set; }
V_Region l;
/// <summary>
/// 根据站点类型和地区类型,获取站点信息
/// </summary>
/// <param name="type"></param>
/// <param name="region"></param>
private void InitializeDataSource()
{
this.SiteTypesDataSource = new ObservableCollection<V_Region>();
getMapDataSoapClient client = new getMapDataSoapClient();
client.getAllSPSitesCompleted += new EventHandler<getAllSPSitesCompletedEventArgs>(client_getAllSPSitesCompleted);
client.getAllSPSitesAsync();
}
void client_getAllSPSitesCompleted(object sender, getAllSPSitesCompletedEventArgs e)
{
ObservableCollection<Video> lists = e.Result;
var tempSTTP = lists.Distinct(new Item_Collection_DistinctBy_VID()).ToList(); //不同的地区
foreach (Video sttp in tempSTTP)
{
this.SiteTypesDataSource.Add(l = new V_Region(sttp.REGIONNM, sttp.VIDEOID));
List<Video> rets = (from li in lists
where li.REGIONNM == sttp.REGIONNM
select li
).ToList<Video>();
foreach (Video item in rets)
{
l.V_Sites.Add(new V_Site(item.SITENM, item.VIDEOID, item.LGTD, item.LTTD));
}
}
this.RadTreeView1.ItemsSource = SiteTypesDataSource;
this.RadTreeView1.ExpandAll();
}
#endregion
6、点击TreeView节点,进行站点定位聚焦
//选中时动态绑定
private void RadTreeView1_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
RadTreeView source = sender as RadTreeView;
try
{
if (source.SelectedItem is MapClient.TreeClass.V_Region)
{
//地区
return;
}
if (source.SelectedItem is V_Site)
{
//站点
V_Site s = source.SelectedItem as V_Site;
Graphic g = new Graphic()
{
Geometry = mercator.FromGeographic(new MapPoint(double.Parse(s.S_LGTD.ToString()), double.Parse(s.S_LTTD.ToString()))),
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as Symbol
};
ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(g.Geometry).Extent; //选中点的位置
double expandPercentage = 2;
//加数值后,聚焦(这里需要注意,进行地理坐标和墨卡托坐标的转换 2013-01-29 张大伟)
double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);
double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);
ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMin - (widthExpand / 2), selectedFeatureExtent.YMin - (heightExpand / 2))), WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMax + (widthExpand / 2), selectedFeatureExtent.YMax + (heightExpand / 2))))
{
SpatialReference = new SpatialReference(102100)
};
try
{
//聚焦
myMap.ZoomTo(displayExtent);
ShowFocus(g);
}
catch (Exception)
{
}
}
}
catch (Exception ex)
{
return;
}
}
7、效果如下图: