[Solved] Highlight links without selecting it

Hi,

I've a scenario where I need to highlight all the links to/from a node when the node is selected. I'm using GraphModel & don't want to use any other model type as there is a requirement to keep link information in the node data.

Is there any way I can change the color of the line & arrow head through code? I thought of changing the link template through code, but, it is not possible.

I’m using goSilverlight 1.2.6.4 & Silverlight 4

Thanks :)

Sure – anything you can do in XAML you can also do in code.

Unfortunately Silverlight 4 does not have multi-bindings, so you cannot data-bind your link’s Path.Stroke to both {Binding Path=Link.FromNode.IsSelected, Converter=…} and {Binding Path=Link.ToNode.IsSelected, Converter=…}. However, if you only need to worry about links connected to a selected node in one direction, this would be the way to go.

If you have added x:Name attributes to the appropriate elements in your Link DataTemplate(s), you can find that element using Part.FindNamedDescendant, cast it to the appropriate class, and set whatever properties you want.

Do something like:

myDiagram.SelectionChanged += (s, e) => {
foreach (Part part in e.RemovedItems) {
Node node = part as Node;
if (node == null) continue;
foreach (Link link in node.LinksConnected) {
Shape path = link.Path;
path.Stroke = … original brush …;
}
}
foreach (Part part in e.AddedItems) {
Node node = part as Node;
if (node == null) continue;
foreach (Link link in node.LinksConnected) {
Shape path = link.Path;
path.Stroke = … highlight brush …;
}
}
};

Thanks Walter.

The ConnectedLinks includes all the links to & from (both direction) the node. Below is the code snippet for highlighting the links:

private void HighlightLinks(Node node, bool isLinksHighlighted)
{
    if (node == null)
        return;

    foreach (Link l in node.LinksConnected)
    {
        l.Path.Stroke = (isLinksHighlighted) ? _linkHighlightBrush : _linkNormalBrush;
        Path p = l.FindNamedDescendant("ArrowHead") as Path; //"ArrowHead" is x:Name of the path representing the pointed end
        if(p != null)
            p.Fill = (isLinksHighlighted) ? _linkHighlightBrush : _linkNormalBrush;
    }
}

Below is code snippet of the diagram’s SelectionChanged event handler:

private void Diagram_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    foreach (Part p in e.RemovedItems){
        Node n = p as Node;
        if (n == null) continue;

        HighlightLinks(n, false);
    }

    foreach (Part p in e.AddedItems){
        Node n = p as Node;
        if (n == null) continue;
        
        HighlightLinks(n, true);
    }
...
}