Я хочу использовать команду delete (Relay Command) для удаления элементов, выбранных в моем древовидном представлении.
У меня есть Main windows .xaml, например, `
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:DirectoryItem}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
<TreeView ItemsSource="{Binding TreeFiles}" Name="treeview2" Grid.Row="5" Grid.Column="1" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:FileItem}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
<Label Grid.Row="1" Grid.Column="4" Content="{Binding Nom}"></Label>
<Label Grid.Row="2" Grid.Column="4" Content="{Binding Version}"></Label>
<Label Grid.Row="3" Grid.Column="4" Content="{Binding Id}"></Label>
<Button Click="ADD" Command="{Binding OpenFolderCommand}" Grid.Row="4" Grid.Column="3" HorizontalAlignment="Left" Width="100" >ADD</Button>
<Button Click="DELETE" Command="{Binding DeleteCommand}" Grid.Row="4" Grid.Column="3" HorizontalAlignment="Stretch" Width="100" >DELETE</Button>`
И модели:
public class Item
{
public string Name { get; set; }
public string Path { get; set; }
public bool IsSelected { get; set; }
}
public class DirectoryItem : Item
{
public List<Item> Items { get; set; }
public DirectoryItem()
{
Items = new List<Item>();
}
}
class FileItem : Item
{
}
и ViewModel с DeleteCommand
public ViewModel()
{
itemprovider = new ItemProvider();
fileprovider = new FileProvider();
}
private List<Item> _treeItems;
public List<Item> TreeItems
{
get { return _treeItems; }
set
{
if (value == _treeItems)
{
return;
}
else
{
_treeItems = value;
OnPropertyChanged("TreeItems");
}
}
}
public ICommand OpenFolderCommand
{
get => new RelayCommand(a => this.OpenFolder());
}
private void OpenFolder()
FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && OpenFolderCommand.CanExecute(openFolderDialog.SelectedPath))
{
FoldernameWithPath = openFolderDialog.SelectedPath;
TreeItems = itemprovider.GetItems(openFolderDialog.SelectedPath);
}
}
public ICommand DeleteCommand
{
get => new RelayCommand(a => this.DeleteFolder());
}
public void DeleteFolder()
{
//TreeItems = itemprovider.GetItems();
foreach (var item in TreeItems)
{
if (item.IsSelected)
{
TreeItems.Remove(item);
}
}
}
И когда я нажимаю кнопку удаления, ничего не происходит, и я новичок в этих технологиях ( WPF, MVVM) пожалуйста, помогите мне