У меня есть список сложных объектов и ListBox, связанный с этим списком с помощью BindingSource.Когда пользователь выбирает элементы в списке, он может редактировать его свойства через PropertyGrid, а текст - отдельно через TextBox.Когда свойство изменяется с помощью PropertyGrid, вызывается CurrentItemChanged BindingSource, но у меня возникают проблемы с обновлением DataBinding, когда пользователи просто редактируют TextBox
Вот код, который лучше объясняет мою ситуацию:
class Song
{
public string Title{get;set}
[Browsable(false)]
public string Text{get;set;}
...
}
class SongBook
{
public List Songs {get;set;}
...
}
// Initialization: we are setting ListBox's DataSource to songBookBindingSource
private void InitializeComponent()
{
...
this.allSongsList.DataSource = this.songBookBindingSource;
...
}
// We create new SongBook object, and set BindingSource's DataSource to
// list of songs in songbook
private void OpenSongBook()
{
...
currentSongBook.Deserialize( path );
songBookBindingSource.DataSource = currentSongBook.Songs;
}
// When user selects a song in ListBox, we try to edit it's properties
private void allSongsList_SelectedValueChanged(object sender, EventArgs e)
{
...
songProps.SelectedObject = allSongsList.SelectedItem;
songTextEdit.Text = (allSongsList.SelectedItem as Song).Text;
}
// This get called whenever user changes something in TextBox.
// If it does, we want to mark song as Unsaved and refresh
// ListBox, so it would display a nice little "*" next to it!
private void songTextEdit_TextChanged(object sender, EventArgs e)
{
currentSong.Text = editSongTextBox.Text;
currentSong.Unsaved = true;
// As far as I understand, this SHOULD make ListBox bound to songBookBindingSource
// update its items. But it does not! How do I make it understand that data changed?
songBookBindingSource.RaiseListChangedEvents = true;
// And if I do this, ListBox DOES gets updated, but something also inserts A COPY OF CURRENT ITEM
// into it. If I select it, allSongsList.SelectedItem throws "Out of bounds" exception. As far
// as I understand, it gets added only to ListBox, but NOT to underlying List. But why is it
// even getting added at all?!
// songBookBindingSource.ResetCurrentItem();
}
Я чувствую, что .NET Framework меня ненавидит:)