当树视图失去焦点时,如何使 KMLTreeView 项保持"选中"状态?



在Google Earth中,单击Globe时,KMLTreeView中所选项目的背景色会变暗。在我基于C#的应用程序中,TreeView节点会失去所有颜色,所以我不知道选择了哪个项目。

类似地,当我点击相关的占位符时,我希望树视图节点高亮显示,就像在GE中一样。

我认为这是默认行为,所以我一定没有正确地将占位符与kmltreeview关联起来。以下是我用于创建节点并将其添加到地球仪以及kmltreeview控件的代码。我是否做错了什么或没有做错什么,以便能够使用默认行为?

谢谢!

dynamic placemark = KmlHelpers.CreatePlacemark(ge1,
Coord,
d.sSerialNumber,
d.sNickname,
"Device Type: " + d.sName + "<p>" +
"IP Address: " + d.sIPAddress + "<p>" +
"ESN: " + d.sSerialNumber + "<p>" +
"<a href="http://localhost/index.html#"
+ d.sSerialNumber + "">Details</a>");
var styleMap = ge1.createStyleMap("");
// Create normal style for style map.
var normalStyle = ge1.createStyle("");
var normalIcon = ge1.createIcon("");
normalIcon.setHref("http://maps.google.com/mapfiles/kml/shapes/truck.png");
normalStyle.getIconStyle().setIcon(normalIcon);
// Create highlight style for style map.
var highlightStyle = ge1.createStyle("");
var highlightIcon = ge1.createIcon("");
highlightIcon.setHref("http://maps.google.com/mapfiles/kml/shapes/truck.png");
highlightStyle.getIconStyle().setIcon(highlightIcon);
highlightStyle.getIconStyle().setScale(2.0);
styleMap.setNormalStyle(normalStyle);
styleMap.setHighlightStyle(highlightStyle);
// Apply stylemap to a placemark.
placemark.setStyleSelector(styleMap);
kmlTreeView.ParseKmlObject(placemark);
KmlTreeView继承自标准TreeView控件,因此您可以使用HideSelection属性。默认情况下,它设置为True,但是。。。

当此属性设置为false时,TreeView中的选定节点控件保持高亮显示为与当前颜色不同的颜色TreeView控件失去焦点时的选择颜色。您可以使用当用户单击表单上的其他控件或移动到不同的窗口。

要在代码中执行此操作,例如:

kmlTreeView.HideSelection = False;

此外,可以在控件的可视化设计器中设置属性,只需选择KmlTreeView,然后查看属性。最后,双击HideSelection将其设置为False

更新:

对于问题的第二部分,即单击协同功能时高亮显示节点,您需要编写一个自定义事件处理程序,然后使用KmlTreeView的GetNodeById方法。下面这样的东西应该会起作用。

private void GEWebBrowser1OnPluginReady(object sender, GEEventArgs geEventArgs)
{
this.geWebBrowser1.AddEventListener(geEventArgs.ApiObject.getGlobe(), EventId.MouseDown);
this.geWebBrowser1.KmlEvent += geWebBrowser1_KmlEvent;
}
void geWebBrowser1_KmlEvent(object sender, GEEventArgs e)
{
// the feature that the mousedown event fired on
dynamic feature = e.ApiObject.getTarget();
// If you have other events added you will need some conditional logic here
// to sort out which event has fired. e.g.
// if(e.EventId == EventId.MouseDown)
// if(GEHelpers.IsApiType(feature, ApiType.KmlPlacemark);
// etc..
string id = feature.getId(); // the features id
KmlTreeViewNode node = myKmlTreeView.GetNodeById(id);   // find the corresponding node...    
if(node == null) { return; } // no corresponding node...
// set the selected node to the feature node.
myKmlTreeView.SelectedNode = node; 
// make sure the node is visible in the treeview
if (!node.IsVisible)
{
node.EnsureVisible();
}
}

最新更新