c# - 当 TreeView 失去焦点时,如何保留 KMLTreeView 项目 'selected'?

标签 c# winforms treeview google-earth-plugin

在 Google 地球中,单击地球仪时,K​​MLTreeView 中所选项目的背景颜色会变暗。在我基于 C# 的应用程序中,TreeView 节点丢失了所有颜色,因此我不知道选择了哪个项目。

同样,我希望在单击关联的地标时突出显示 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 control ,因此您可以使用 HideSelection属性(property)。默认情况下,此设置为 True,但是...

When this property is set to false, selected nodes in the TreeView control remain highlighted in a different color than the current selection color when the TreeView control loses focus. You can use this property to keep items that are selected by the user visible when the user clicks a different control on the form or moves to a different window.

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

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();
    }
}

关于c# - 当 TreeView 失去焦点时,如何保留 KMLTreeView 项目 'selected'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13589700/

相关文章:

c# - 如何延迟 Button.Flyout 的出现?

c# - Windows 窗体 TreeView 始终选择焦点上的节点

c# - 通过业务逻辑初始化 Linq to Sql 对象

c# - 为什么 C# 没有非虚拟调用约定?

c# - 如何为两个 Windows 窗体应用程序共享一个 IsolatedStorage?

c# - 带复制粘贴的 ListView

c# - 如何重写 TreeNodeCollection.Add(TreeNode)?

JavaFX : how to handle dragging an item from a TreeView

c# - Unity3d:CommandInvokationFailure:Gradle构建失败

c# - C#Errorprovider.ContainerControl.Controls仅包含TabControl