Disable multiple links selection

Hi Walter,

How can I disable multiple selection of the links in the graph?
I want user should be able to select only one link.
Thanks and Regards,
Hardik

You can limit the total number of parts selected at a time by setting Diagram.MaximumSelectionCount.

Hi Walter,

Thanks for quick reply.
I want to allow multi select in Node but Single select in Links.
How can I achieve it?
Regards,
Hardik

I’m not sure there’s an easy solution.
I think you’ll have to override Link.OnIsSelectedChanged to see if there’s two (or more) Links selected – if so, set Link.IsSelected = false.

In order for your diagram to make use of your custom Link-derived class, you’ll need to customize the Diagram.PartManager. Override this method:

protected <font color="#FF0000">override</font> Link MakeLinkForData(Object linkdata, IDiagramModel model, DataTemplate templ, String category) { Link link = new <font color="#FF0000">MyLink</font>(); PartBinding data = new PartBinding(link, linkdata); link.Content = data; link.DataContext = data; link.ContentTemplate = templ; if (category != null && category != "") { if (link.Category != category) link.Category = category; } return link; }
I have marked the needed changes in RED.

I hope this works for you.

Sorry for long delay. I didn’t got chance to implement this due to some reasons.

I am trying to extend the Link as below

public class MyLink : Link<?: prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

protected override void OnIsSelectedChanged()

{

base.OnIsSelectedChanged();

}

}

How can I check if multiple links are selected? Can you please provide another code snippet?

Thanks a lot,

Hardik

Here’s how I would find out if there are multiple links that are selected:

myDiagram.SelectedParts.OfType<Link>().Where(x => x.IsSelected).Count() > 1

I’m sure you could have figured out some way to do this yourself.

Hi Walter, <?: prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Please forgive my ignorance but I am not getting how to go ahead wiht this.

You had suggested to extend Link class and to override the OnIsSelectedChanged in yout post dated 25-Jul.

Do I need to extend PartManager class also?

How Do I hook the new extended class to my graph?

Where should I put the code which you provided now? [myDiagram.SelectedParts.OfType().Where(x => x.IsSelected).Count() > 1]

I am very confused. Could you please help me?

Thanks and Regards

I gave you the code for extending PartManager above – that’s how you make use of the custom MyLink class. Install the custom PartManager by setting Diagram.PartManager with an instance of your class.

Hi Walter,

I have tried your suggestion but I could not make it work due to my lack of knowledge.
Finally I tried following (handining the SelectionChanged event of control) and it seems working. Could you please review it and point if there is any issue which may cause problem?

private void tsGraph_SelectionChanged(object sender, SelectionChangedEventArgs e)<?: prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

//If new items added

if (e.AddedItems != null)

{

//Check if the multiple link nodes selected

if (tsGraph.SelectedParts.OfType<Link>().Where(x => x.IsSelected).Count() > 1)

{

//Deselect the newly added links

Dispatcher.BeginInvoke(new Action(() =>

{

foreach (object addedObject in e.AddedItems)

{

if (addedObject is Link)

{

((Link)addedObject).IsSelected = false;

}

}

}

));

return;

}

}

}

Thanks and Regards,
Hardik

Ah, that’s clever – you are letting the selection happen anyway but then removing excess ones later.

If that’s OK with your application logic, that strategy seems OK to me. The only issue here is if there are conflicting de-selection changes before your Action happens “later”. It might be wiser to check the number of selected links in your Action.