The problem is most likely linked. ListBox does not add items.

<Window x:Class="WpfApplication2.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:WpfApplication2" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:MyList x:Key="MyData"/> <DataTemplate x:Key="temp"> <Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4" Width="370"> <Grid Margin="3"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Grid.RowSpan="2" Width="100" Height="75" Margin="6" Source="{Binding Path=Img}"/> <StackPanel Grid.Column="1" Margin="2,6"> <TextBlock Text="{Binding Path=Name}"/> <TextBlock Text="{Binding Path=Surname}"/> <TextBlock Text="{Binding Path= Country}"/> </StackPanel> </Grid> </Border> </DataTemplate> </Window.Resources> <Grid> <ListBox ItemTemplate="{StaticResource temp}" ItemsSource="{Binding Source={StaticResource MyData}}" x:Name="listBox" HorizontalAlignment="Left" Height="258" Margin="10,10,0,0" VerticalAlignment="Top" Width="358"/> <Button x:Name="Add" Content="Add" HorizontalAlignment="Left" Margin="388,85,0,0" VerticalAlignment="Top" Width="75" Click="Add_Click"/> <Button x:Name="Edit" Content="Edit" HorizontalAlignment="Left" Margin="388,110,0,0" VerticalAlignment="Top" Width="75" Click="Edit_Click"/> <Button x:Name="Remove" Content="Remove" HorizontalAlignment="Left" Margin="387,135,0,0" VerticalAlignment="Top" Width="75" Click="Remove_Click"/> </Grid> 

Here is the class itself:

 public partial class MainWindow : Window { private Window1 wd; private MyList MyData; public MainWindow() { InitializeComponent(); MyData = new MyList(); MyData.Add(new Person("1", "2", "3", null)); MyData.Add(new Person("1", "2", "3", null)); } } class MyList : ObservableCollection<Person> { public ObservableCollection<Person> MyData; public MyList() : base() { MyData = new ObservableCollection<Person>(); MyData.Add(new Person("1", "2", "3", null)); MyData.Add(new Person("1", "2", "3", null)); } } class Person { public string Name { get; set; } public string Surname { get; set; } public string Country { get; set; } public Image Img { get; set; } public Person(string name, string surname, string country, Image img) { Name = name; Surname = surname; Country = country; Img = img; } public Person() { } } 
  • one
    What do you want to get? What you can see are the two Person elements in the MyData sheet. Generally there is some confusion. First, outside you declare a variable MyData of type MyList, then declare a variable MyData of type ObservableCollection <Person>. Logic is absolutely tangled here - Garrus_En
  • and where you set DataContext ? - Ev_Hyper

1 answer 1

Where do you find such ways to create collections?)

Well, correct what we have:

 <local:MyList x:Key="MyData"/> 

The object is created in the window resource, respectively, adding items to the collection should occur in the constructor of this object. The fact that you add elements in a * .cs file is not exactly because it is not the object to which you are attached. (The binding goes to the object created in the resource).

Go ahead. Since you decided to create a collection inherited from itself, then add elements using the inherited method:

 public class MyList : ObservableCollection<Person> { public MyList() : base() { Add(new Person("1", "2", "3", null)); Add(new Person("1", "2", "3", null)); } } 

In principle, everything should work.


A couple of comments on your code:

 public ObservableCollection<Person> MyData; 

If you bind to MyData, the binding will not work. Binding works only with properties, i.e. should look like this:

 public ObservableCollection<Person> MyData { get; set; } 

As it is done usually :

The project must have a VM class that implements the INotifyPropertyChanged interface. This class will be the DataContext-ohm for your window ( how to set the DataContext ). In this class and declare the list:

 public class MainVM : INotifyPropertyChanged { public ObservableCollection<Person> MyList { get; set; } public MainVM() { MyList = new ObservableCollection<Person>(); MyList.Add(new Person("1", "2", "3", null)); MyList.Add(new Person("1", "2", "3", null)); } //--------------------- //реализация интерфейса } //--------------------- 

After such manipulations, it is a pleasure to use a binding:

 <ListBox ItemsSource="{Binding MyList}"/> 
  • one
    Thank you, everything works, but how to add or edit elements in this case? - Adam Hodovanets
  • You are welcome. I added the answer, look. - Gardes