Question

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; }
}
Was it helpful?

Solution

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; }
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top