views:

542

answers:

2

Hello, Does anyone know why my ListView with following Code is not working? I checked it out with Snoop and the ItemsSource seems to be fine (and when I start Snoop, the ListView displays me the MyViewModel.MyCollection, but when debugging with Visual Studio it shows me nothing?)

Thank you!

PS: MainWindow.xaml.cs has the DataContext = MainViewModel

    <ListView Grid.Row="1" Margin="38,50,0,168" HorizontalAlignment="Left" Name="listViewSelectDate" Width="105"
              ItemsSource="{Binding Path=MyViewModel.MyCollection}" 
              SelectedItem="{Binding SelectedDate}" IsSynchronizedWithCurrentItem="True">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=CalcDate}"/>
            </GridView>
        </ListView.View>
    </ListView>

The ViewModel looks like this:

class MainViewModel : ViewModelBase
{
    public SummaryViewModel MyViewModel
    {
        get { return _myViewModel; }
        set { _myViewModel = value; RaisePropertyChanged("MyViewModel"); }
    }
    public MyDate SelectedDate
    {
        get { return _selectedDate; }
        set { _selectedDate = value; RaisePropertyChanged("SelectedDate"); }
    }
}

and

public class SummaryViewModel : ViewModelBase
{
    public ObservableCollection<MyDate> MyCollection { get; set; }
}

and

public class MyDate
{
    public DateTime CalcDate { get; set; }
}
+1  A: 

Who sets MyCollection? It is not providing change notification, so the binding doesn't know that it has been changed. Change to:

private ObservableCollection<MyDate> _myCollection;
public ObservableCollection<MyDate> MyCollection
{
    get { return _myCollection; }
    set
    {
        _myCollection = value;
        OnPropertyChanged("MyCollection");
    }
}

Or, even better, make it read-only:

private readonly ICollection<MyDate> _myCollection = new ObservableCollection<MyDate>();

public ICollection<MyDate> MyCollection
{
    get { return _myCollection; }
}

HTH, Kent

Kent Boogaart
Actually I thought ObservableCollection doesn't need a OnPropertyChanged (in my case RaisePropertyChanged), since it already implements INotifyPropertyChanged (http://msdn.microsoft.com/de-de/library/ms668604.aspx). Anyways, I tried that too and it doesn't seem to work..
Joseph Melettukunnel
My mistake, you were right! Thanks a lot
Joseph Melettukunnel
A: 

Look in the Visual Studio Output window, it will show any DataBinding errors that you may be getting which may help you resolve the issue.

Sijin
Which Window do you mean? :-) I didn't know that Visual Studio can display WPF DataBindings
Joseph Melettukunnel
The Output window.
Karim