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; }
}
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; }
}
在“ Visual Studio输出”窗口中查找,它将显示您可能遇到的任何DataBinding错误,这可能有助于您解决问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.