0
Reply

Data binding problem

Stephan J

Stephan J

Sep 1 2011 4:22 AM
1.3k
I'm having trouble binding my ListBox with the weatherForecasts objects.

I don't know what is wrong with the binding, but nothing shows up. I want it to take all of the items in the collection from WeatherForecastController class and output them in the ListBox

XAML code:

<Window x:Class="WeatherApp.WeatherWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WeatherApp"
xmlns:model="clr-namespace:WeatherApp.Model"
Title="Weather app" Height="300" Width="500">

<window.DataContext >
<model:WeatherForecastController />
</window.DataContext>

<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Top">
<Button Content="Update" Width="50" Height="25" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Top" />
</Grid>
<ListBox Name="listbox1" ItemsSource="{Binding Path=WeatherForecasts}">
<Label Content="{Binding Path=WeatherForecasts/DayOfTheWeek}" />
</ListBox>
</DockPanel>
</Window>


 Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading;

namespace WeatherApp.Model {
public class WeatherForecastController {
private ObservableCollection<WeatherForecast> weatherForecasts = new ObservableCollection<WeatherForecast>();

public WeatherForecastController() {
weatherForecasts.Add(new WeatherForecast());
}

internal ObservableCollection<WeatherForecast> WeatherForecasts {
get { return weatherForecasts; }
set { weatherForecasts = value; }
}
}
}

WeatherForecast class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeatherApp.Model {
internal class WeatherForecast {
private string dayOfTheWeek = "Monday";
private string message = "Varmt!";

public string Message {
get { return message; }
}

public string DayOfTheWeek {
get { return dayOfTheWeek; }
}
}
}