Reputation: 10587
I have a ListBox showing some information about Cars. So, I have ViewModel with:
private ObservableCollection<Car> _cars;
public ObservableCollection<Car> CarsList
{
get
{
return _cars;
}
set
{
_cars= value;
OnPropertyChanged("CarsList");
}
}
I have implemented PropertyChanged, and I get my list of cars just fine. I load my CarsList from within my ViewModel (not from Window).
My Car object has Name, Country, Url and Image object like below:
public class Car
{
public string Name { get; set; }
public string Country { get; set; }
public Image Image { get; set; }
}
public class Image
{
public string Url { get; set; }
}
I need to bind Name, Country and Image.Url to text boxes in my ListBox. I did this like:
<ListBox Name="lbEvents" ItemsSource="{Binding Path=CarsList}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<StackPanel>
<TextBlock Name="txtCarName" Text="{Binding Path=Name}" />
<TextBlock Name="txtCarCountry" Text="{Binding Path=Country}"/>
<!-- How do I bind this properly? -->
<Image Name="imgCarImage" Source="{Binding Path=Car.Image.Url}" />
</StackPanel>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Problem is that my Car Name and Country are properly displayed in ListBox for each Car. But Car.Image.Url shows nothing. I am new to WPF, how do I bind properly the Image.Url to the ImageBox?
Upvotes: 0
Views: 120
Reputation: 32515
Just as Name
and Country
are properties of the Car
class, so is Image
. Look at the way you are binding to Name
and Country
, you don't specify it as Car.Name
, so why do that for Image
? Just do it as Image
instead of Car.Image
.
Upvotes: 1