Witajcie!
Mam Listboxa:
<ListBox Name="hTravelList" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Background="#FFF1E9E9" SelectionChanged="hTravelList_SelectionChanged" ItemsSource="{Binding}"/>
oraz IList, który jest zapisany w datacontex ListBoxa:
public IList<cTravel> TravelList = new ObservableCollection<cTravel>();
Klasa cTravel wygląda tak:
namespace Wyszukiwarka_polaczen.Travel
{
public class cTravel : INotifyPropertyChanged
{
// Property Changed
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
// Konstruktor
public cTravel(cConnection FirstConnection)
{
ConnectionList.Add(FirstConnection);
UpdateName();
return;
}
public IList<cConnection> ConnectionList = new ObservableCollection<cConnection>();
public void Add(cConnection Connection)
{
if(ConnectionList.Last().PlaceTo == Connection.PlaceFrom)
{
ConnectionList.Add(Connection);
UpdateName();
}
}
public void Add(cData DataBase, cPlace NextPlace)
{
cConnection Connection = DataBase.ConnectionList.ToList().Find(
delegate(cConnection it)
{
return (it.PlaceFrom == ConnectionList.Last().PlaceTo && it.PlaceTo == NextPlace);
}
);
if(Connection != null)
{
Add(Connection);
return;
}
ConnectionData ConnectionData = new ConnectionData();
ConnectionData.From = ConnectionList.Last().PlaceTo;
ConnectionData.To = NextPlace;
ConnectionDialog Dialog = new ConnectionDialog(DataBase, ConnectionData);
Dialog.ShowDialog();
if(ConnectionData.valid)
{
DataBase.ConnectionList.Add(new cConnection(ConnectionData.From, ConnectionData.To, ConnectionData.TravelTime, ConnectionData.BreakTime));
ConnectionList.Add(DataBase.ConnectionList.Last());
}
}
private string name { get; set; }
public string Name
{
get
{
return name;
}
private set
{
name = value;
OnPropertyChanged("Name");
}
}
public override string ToString()
{
return Name;
}
private void UpdateName()
{
Name = ConnectionList.First().PlaceFrom.Place + " - " + ConnectionList.Last().PlaceTo.Place;
}
}
}
Póki co binding korzysta z ToString() i niestety przy aktualizacji wartości Name nie zmienia się tekst elementu w ListBoxie. W jaki sposób zbindować ListBox z kolekcją, aby do wyświetlania tekstu elementu ListBoxa wykorzystywała stringa Name?