Как сделать в ListView список Title с крестиком напротив, чтобы по нажатию на крестик Title удалялся (wpf mvvm)?
Если, через контекстное меню делаю или просто через кнопку работает, но как сделать так чтобы напротив текста был крестик который удалял бы этот самый item хз xaml:
<Window x:Class="NotesTask.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NotesTask"
mc:Ignorable="d"
xmlns:viewModel="clr-namespace:NotesTask.ViewModel"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<viewModel:NotesViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="28*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<ListView ItemsSource="{Binding Path=NotesModel.Notes}" SelectedItem="{Binding Path=SelectedNote}" Margin="10,10,10,10">
<ListView.ItemTemplate>
<DataTemplate x:Name="NoteTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" MinHeight="25" MinWidth="100" Margin="3,3,3,3" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Command="{Binding Path=RemoveCommand}" CommandParameter="{Binding Path=SelectedNote}" Content="X" MinHeight="25" MinWidth="25" Margin="3,3,3,3" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Command="{Binding Path=RemoveCommand}"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
<Button Grid.Row="1" MinHeight="25" MinWidth="50" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Delete" Command="{Binding RemoveCommand}"/>
</Grid>
</Window>
ViewModel:
using System;
using System.Windows.Input;
using NotesTask.Model;
namespace NotesTask.ViewModel
{
internal class NotesViewModel
{
public Note InputedNote { get; set; }
public Note SelectedNote { get; set; }
public NoteModel NotesModel { get; set; } = new NoteModel();
public ICommand AddCommand { get; set; }
public ICommand RemoveCommand { get; set; }
public NotesViewModel()
{
AddCommand = new RelayCommand(
execute =>
{
if (InputedNote != null)
{
NotesModel.Add(new Note(InputedNote.Title, InputedNote.Description));
}
},
canExecute =>
{
if (InputedNote.Title != "")
{
return true;
}
else
{
return false;
}
}
);
RemoveCommand = new RelayCommand(
execute =>
{
if (SelectedNote != null)
{
NotesModel.Remove(SelectedNote);
}
},
canExecute => SelectedNote != null
);
}
}
}
Источник: Stack Overflow на русском