foreach (var nd in xnn.SelectedNodes)
{
nd.Visibility = System.Windows.Visibility.Collapsed;
}
This topic demonstrates how to remove a node from the network layout, and explains how to remove the node from the data source.
The topic is organized as follows:
The example in this topic demonstrates how to remove a node from NetworkNodeLayout and from the data source.
To begin, please read the Getting Started with xamNetworkNode topic as this tutorial uses the code from the 'Getting Started' topic as a starting point.
The data source must implement the INotifyPropertyChanged
interface in order to implement the functionality to remove a node. This is necessary so the changes are broadcast to the underlying data source and updates can execute.
Nodes can be removed by simply selecting a node, and in the SelectedNodeCollectionChanged event setting the node’s Visibility property to "Collapsed". This does not remove the nodes from the data source. It hides the selected nodes from the view.
In C#:
foreach (var nd in xnn.SelectedNodes)
{
nd.Visibility = System.Windows.Visibility.Collapsed;
}
In Visual Basic:
For Each nd As var In xnn.SelectedNodes
nd.Visibility = System.Windows.Visibility.Collapsed
Next
Removing a node can also be accomplished by implementing a KeyDown handler to respond to the pressing of the Delete key on the keyboard after the node is selected.
In C#:
private void xnn_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (xnn.SelectedNodes.Count > 0)
{
xnn.SelectedNodes[0].Visibility = System.Windows.Visibility.Collapsed;
}
}
}
In Visual Basic:
Private Sub xnn_KeyDown(sender As Object, e As KeyEventArgs)
If e.Key = Key.Delete Then
If xnn.SelectedNodes.Count > 0 Then
xnn.SelectedNodes(0).Visibility = System.Windows.Visibility.Collapsed
End If
End If
End Sub
The following images demonstrate the outcome when nodes are removed from the network layout.
Initial layout:
Figure 1
Set node 8’s Visibility to "Collapsed":
Figure 2
Set node 5’s Visibility to "Collapsed". Notices that the nodes (6, and 7) connected to the node '5' appear, but detached because the node they are directly connected to is hidden.
Figure 3
The xamNetworkNode listens for changes in the nodes collection to remove nodes from the data model by change notification. The control is notified of any changes made in the nodes' collection, and gets updated reflective of the changes from the data model. The data object must implement INotifyPropertyChanged
interface for the data source to get updated.
The following example demonstrates removing the first node from the data source.
In C#:
ObservableCollection<NodeModel> nMod =
xnn.ItemsSource as ObservableCollection<NodeModel>;
nMod.RemoveAt(0);
In Visual Basic:
Dim nMod As ObservableCollection(Of NodeModel) = _
TryCast(xnn.ItemsSource, ObservableCollection(Of NodeModel))
nMod.RemoveAt(0)