Please tell me how to make a binding so that the data of the selected object is displayed in the TextBox
<Window x:Class="WpfApp.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:WpfAppExam" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Orientation="Vertical"> <TextBlock Text="Список продуктов" TextDecorations="Underline" Margin="20" TextAlignment="Center" FontSize="20"/> <ListBox Name="list" DisplayMemberPath="Name" Padding="20" FontSize="16" SelectionChanged="List_SelectionChanged"> </ListBox> </StackPanel> <StackPanel Grid.Column="1" Orientation="Vertical"> <TextBlock Name="tbNameProduct" Margin="20" TextAlignment="Center" FontSize="20"/> <StackPanel Orientation="Horizontal"> <CheckBox Name="checkBox" IsChecked="False" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Margin="20"/> <TextBlock Text="Купил" Margin="0,20"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBox MinWidth="20" Margin="20" Text="{Binding Path=Count}"/> <TextBlock Text="Количество" Margin="0,20"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBox Name="tbPrice" MinWidth="20" Margin="20" IsReadOnly="True" Text="{Binding Path=Price}"/> <TextBlock Text="Предполагаемая цена" Margin="0,20"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBox MinWidth="20" Margin="20" Text="{Binding Path=RealPrice}"/> <TextBlock Text="Реальная цена" Margin="0,20"/> </StackPanel> </StackPanel> </Grid> </Window> xaml.cs
namespace WpfApp { public partial class MainWindow : Window { int index; public MainWindow() { InitializeComponent(); List<Product> products = new List<Product> { new Product("Bread",2,15.5), new Product("Milk",1,30), new Product("Sugar",1,25) }; list.ItemsSource = products; } private void List_SelectionChanged(object sender, SelectionChangedEventArgs e) { Product p = (Product)list.SelectedItem; tbNameProduct.Text = p.Name; index = list.SelectedIndex; } private void CheckBox_Checked(object sender, RoutedEventArgs e) { ListBoxItem lbi = list.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; lbi.Foreground = Brushes.Yellow; Product p = (Product)list.SelectedItem; p.Purchased =true; } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { ListBoxItem lbi = list.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; lbi.Foreground = Brushes.Black; Product p = (Product)list.SelectedItem; p.Purchased = false; } } } and Product class
class Product: INotifyPropertyChanged { string name; double count; double price; double realPrice; bool purchased; int code; public Product() { name = null; count = 0; price = 0; realPrice = 0; purchased = false; code = 0; } public Product(string Name,double Count, double Price ) { name = Name; count = Count; price = Price; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } public string Name { get { return name; } set { name = value; OnPropertyChanged("name"); } } public double Count { get { return count; } set { count = value; OnPropertyChanged("count"); } } public double Price { get { return price; } set { price = value; OnPropertyChanged("price"); } } public double RealPrice { get { return realPrice; } set { realPrice = value; OnPropertyChanged("realPrice"); } } public bool Purchased { get { return purchased; } set { purchased = value; OnPropertyChanged("purchased"); } } public int Code { get { return code; } set { code = value; OnPropertyChanged("code"); } } }