I want to delete an item from an HierarchicalDataTemplate. I am using a data source. and due to that I can just delete an item from the treeview I need to remove it from it souce
what will be the logic to delete such a thing will appericiate some help.
WPF Code:
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type cft:CommadStructure}" ItemsSource="{Binding Items}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Index,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" />
<TextBlock Text="{Binding Path=NameOfCommand,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.Resources>
C# try...:
private void RemoveOneItem_Click(object sender, RoutedEventArgs e)
{
TRVCommands.Items.Remove(TRVCommands.SelectedItem);
string index = (TRVCommands.SelectedItem as ClassForTest.CommadStructure).Index;
try
{
ObservableCollection<ClassForTest.CommadStructure> target = _OCommandStructure;
for (int i = 0; i < target.Count; i++)
{
if (target[i].Index == index)
{
target.Remove((TRVCommands.SelectedItem as ClassForTest.CommadStructure));
TRVCommands.Items.Refresh();
return;
}
ObservableCollection<ClassForTest.CommadStructure> NewTarget = _OCommandStructure[i].SubCommandStructure;
NewTarget = target[i].SubCommandStructure;
while (NewTarget.Count > 0)
{
for (int j = 0; j < NewTarget.Count; j++)
{
if (NewTarget[i].Index == index)
{
NewTarget.Remove((TRVCommands.SelectedItem as ClassForTest.CommadStructure));
TRVCommands.Items.Refresh();
return;
}
}
NewTarget = NewTarget[]
}
}
}
catch
{
MessageBox.Show("There is an error on index.");
return;
}
}

so the logic should be an recurse method that is checking via index/Name if this is the item and delete it for its item source.
C# code1:
C# Code2: