首先设计页面
<telerik:RadTreeView x:Name="radTreeView" Margin="8"
IsLoadOnDemandEnabled="True" IsExpandOnSingleClickEnabled="True"
LoadOnDemand="radTreeView_LoadOnDemand"/>
页面载入的逻辑,其中优先定义全局变量
private EmiteServiceContractClient wsEmiteClient;
private RadTreeViewItem treeViewItem=new RadTreeViewItem();
private ObservableCollection<MetricNode> RootNode { get; set; }
private ObservableCollection<MetricNode> ChildNode { get; set; }
调用WebService,设置接入的地址,以及连接登入,然后执行方法
public MainPage()
{
InitializeComponent();
wsEmiteClient = new EmiteServiceContractClient();
wsEmiteClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri(App.webServiceURL));
TempToken.CreateScopeAndSecurityToken(wsEmiteClient.InnerChannel);
wsEmiteClient.GetRootNodes_OneView2Completed -= wsEmiteClient_GetRootNodes_OneView2Completed;
wsEmiteClient.GetRootNodes_OneView2Completed += wsEmiteClient_GetRootNodes_OneView2Completed;
wsEmiteClient.GetRootNodes_OneView2Async();
}
当radTreeView控件执行载入时触发逻辑。首先调用webservice获得余下子节点,然后获得点击的RadTreeViewitem项,并执行获得子节点的方法
private void radTreeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
wsEmiteClient.GetChildNodes_OneView2Completed -= wsEmiteClient_GetChildNodes_OneView2Completed;
wsEmiteClient.GetChildNodes_OneView2Completed += wsEmiteClient_GetChildNodes_OneView2Completed;
RadTreeViewItem clickItem = e.OriginalSource as RadTreeViewItem;;
treeViewItem = clickItem;
GetChildrenMetrics2(clickItem.Tag.ToString());
}
private void AddTreeViewItems()
{
foreach (MetricNode m in RootNode)
{
RadTreeViewItem category = new RadTreeViewItem();
(category).Header = m.MetricName;
category.Foreground = new SolidColorBrush(Colors.Green);
category.Tag = m.NodeId;
radTreeView.Items.Add(category);
}
}
private void wsEmiteClient_GetRootNodes_OneView2Completed(object sender, GetRootNodes_OneView2CompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result != null && e.Result.Count > 0)
{
RootNode = e.Result;
AddTreeViewItems();
}
}
else
{
//Common.LogError(ErrorHandler.GetFullErrorMessage(e.Error, "SvcModelViewModel"));
}
}
private void GetChildrenMetrics2(string parentId)
{
TempToken.CreateScopeAndSecurityToken(wsEmiteClient.InnerChannel);
wsEmiteClient.GetChildNodes_OneView2Completed -= wsEmiteClient_GetChildNodes_OneView2Completed;
wsEmiteClient.GetChildNodes_OneView2Completed += wsEmiteClient_GetChildNodes_OneView2Completed;
wsEmiteClient.GetChildNodes_OneView2Async(new MetricNodeRequest() { NodeId = int.Parse(parentId) });
}
private void wsEmiteClient_GetChildNodes_OneView2Completed(object sender, GetChildNodes_OneView2CompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result != null && e.Result.Count > 0)
{
ChildNode = e.Result;
if (ChildNode!=null&&ChildNode.Count>0)
{
foreach (MetricNode metricNode in ChildNode)
{
RadTreeViewItem product = new RadTreeViewItem();
product.Header = metricNode.MetricName;
product.Tag = metricNode.NodeId;
treeViewItem.Items.Add(product);
}
}
}
}
else
{
//Common.LogError(ErrorHandler.GetFullErrorMessage(e.Error, "SvcModelViewModel"));
}
}