listBox wpf c# sql вывод информации конкретного элемента по клику
у меня есть listBox хочу чтобы при нажатии на кнопку,которая содержится в каждом элементе выводилась инфомрация по конкретному элементу,вся инфа в бд хранится,в заголовках элемента находится поле NameOfAdvert и DateOfPublished также в бд есть поле Content где находится информация по элементу,мне необходимо как-то выводить эту инфу по клику,допустим,в MessageBox.Show(advert.Content); если честно,когда я дошел до этого,я понял,что ничего не могу придумать,прошу помочь решить эту проблему View:
WpfApp1:ContextHolder>
<WpfApp1:ContextHolder.Commands>
<WpfApp1:CommandBinding RoutedCommand="{x:Static ApplicationCommands.Delete}"
RelayCommand="{Binding RemoveCommand}"/>
<WpfApp1:CommandBinding RoutedCommand="{x:Static ApplicationCommands.Open}"
RelayCommand="{Binding ShowContentCommand}"/>
</WpfApp1:ContextHolder.Commands>
<ListBox x:Name="ListBoxName" SelectedIndex="{Binding SelectetListBoxIndex}"
SelectedItem="{Binding SelectetListBoxItem}"
ItemsSource="{Binding ItemForListBox}"
HorizontalAlignment="Left" Height="319" Margin="81,42,0,0"
VerticalAlignment="Top" Width="238">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<TextBlock FontSize="16" Text="{Binding Path=NameOfAdvert}" HorizontalAlignment="Left" />
<TextBlock FontSize="16" Text="{Binding Path=DateOfPublished}" HorizontalAlignment="Left" />
<Button Width="150" Height="30" Content="Удалить"
Command="{x:Static ApplicationCommands.Delete}"
CommandParameter="{Binding}"
HorizontalAlignment="Left"/>
<Button Width="150" Height="30" Content="Читать"
Command="{x:Static ApplicationCommands.Open}"
CommandParameter="{Binding}"
HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</WpfApp1:ContextHolder>
**ViewModel**:
public class UserMainWindowVm
{
Advert advert = new Advert();
public ObservableCollection<Advert> ItemForListBox { get; set; }
UserContext db;
public Advert SelectetListBoxItem { get; set; }
public Advert SelectetListBoxIndex { get; set; }
private RelayCommand _removeCommand;
private RelayCommand _showContent;
public UserMainWindowVm()
{
ItemForListBox = new ObservableCollection<Advert>();
db = new UserContext();
db.AdvertsSet.Load();
ItemForListBox = db.AdvertsSet.Local;
}
public RelayCommand RemoveCommand => _removeCommand ?? (_removeCommand = new RelayCommand(RemoveMetod));
public RelayCommand ShowContentCommand => _showContent ?? (_showContent = new RelayCommand(ShowContentMetod));
private void RemoveMetod(object parameter)
{
if (parameter is Advert item)
{
Advert adv = SelectetListBoxItem;
db.AdvertsSet.Remove(item);
}
}
private void ShowContentMetod(object parameter)
{
//
MessageBox.Show();
}
}
UserContext
class UserContext:DbContext
{
public UserContext() : base("DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Advert> AdvertsSet { get; set; }
}
Прошу подсказку как реализовать