How to force that a least one node is selected

how do i force that at least one part is selected?
if the user press with the mouse button in an empty place then there are no selected parts.
and i want that if there are no selected part then to select a Specific part.
can you tell me how to do that?

Well, that might be difficult when there are no parts at all.

But the behavior you are talking about, a background left click with no control or shift modifier, is implemented by DiagramTool.StandardMouseSelect which is called by the Diagram.ClickSelectingTool.

So you could replace the standard Diagram.ClickSelectingTool with a custom one that overrides StandardMouseSelect to do what you want. The normal behavior is to call Diagram.ClearSelection() when IsLeftButtonDown() && !IsControlKeyDown() && !IsShiftKeyDown(). Maybe you’ll want to override StandardMouseSelect to be a no-op in that case; otherwise call the base method. However, I don’t know what your application needs to do.

Caution: changing the standard click selection behavior is not going to guarantee that the Diagram.SelectedParts collection is never cleared. There are a lot of other reasons that Diagram.ClearSelection() is called.

I will speculate that the only one that you would care about is when the user hits the ESCape key. The way to change that behavior is to replace the standard Diagram.CommandHandler with one that overrides StopCommand. Here’s what you could try:

public override void StopCommand() { Diagram diagram = this.Diagram; if (diagram == null) return; IDiagramTool tool = diagram.CurrentTool; //if (tool is ToolManager && diagram.AllowSelect) { // diagram.ClearSelection(); //} if (tool != null) { tool.DoCancel(); } }
The commented-out code is what the Stop command would normally do.