Операция недопустима, когда ItemsSource используется. Вместо этого открывайте и изменяйте элементы с помощью ItemsControl.ItemsSource
Я новичок в Binding и WPF. Недавно я узнал, как создатьlistBox
с несколькими столбцами с использованием технологии Binding
<ListView ItemsSource="{Binding Items}" Margin="306,70,22,17" MouseDoubleClick="listBoxSS_MouseDoubleClick" Name="listBoxSS" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="first_name " Width="100" DisplayMemberBinding="{Binding Path=First_name}" />
<GridViewColumn Header="last_name" Width="100" DisplayMemberBinding="{Binding Path=Last_name}" />
<GridViewColumn Header="phone_number" Width="100" DisplayMemberBinding="{Binding Path=Phones[0]}" />
<GridViewColumn Header="notes" Width="100" DisplayMemberBinding="{Binding Path=Notes}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
и это код:
List<Student> arr = search.students();
listBoxSS.ItemsSource = arr;
но проблема была, когда я пытался использовать добавить или удалить элемент или очистить
listBoxSS.Items.Clear();
Пожалуйста, мне нужен пример использования источника элементов или способа добавления, удаления элемента или очистки списка.
EDIT:
<ListView ItemsSource="{Binding Items}" Margin="306,70,22,17" MouseDoubleClick="listBoxSS_MouseDoubleClick" Name="listBoxSS" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="first_name " Width="100" DisplayMemberBinding="{Binding Path=First_name}" />
<GridViewColumn Header="last_name" Width="100" DisplayMemberBinding="{Binding Path=Last_name}" />
<GridViewColumn Header="phone_number" Width="100" DisplayMemberBinding="{Binding Path=Phones[0]}" />
<GridViewColumn Header="notes" Width="100" DisplayMemberBinding="{Binding Path=Notes}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
и вот код:
ObservableCollection<Employee> Gemployees;
var employees = new ObservableCollection<Employee>(search.employees());
search.employees()
получить список всех сотрудников в моей БД
listBoxPE.ItemsSource = employees;
Gemployees = employees;
теперь я могу выполнять все методы на Gemployees
Gemployees.Remove((Student)listBoxSS.SelectedItem);
Gemployees.Add((Student)listBoxSS.SelectedItem);
ListView
обновлять всякий раз, когда я добавляю или удаляю Предмет из Gemployees !! Круто, но все еще немного тяжелая работа по связыванию. Сейчас я делаю интерфейсный класс для каждого ListView, чтобы я мог поместить свои вещи в него. Это не будет выполнять какую-либо гибкость в добавлении предметов.