C#/WPF:ListView 未更新(但当我检查 Snoop 时,一切看起来都很好)
有谁知道为什么我的 ListView 与以下代码不起作用? 我用 Snoop 检查了一下,ItemsSource 似乎没问题(当我启动 Snoop 时,ListView 显示 MyViewModel.MyCollection,但是当用 Visual Studio 调试时,它什么也没显示?)
谢谢!
PS:MainWindow.xaml.cs 具有 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>
如下所示:
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"); }
}
}
和
public class SummaryViewModel : ViewModelBase
{
public ObservableCollection<MyDate> MyCollection { get; set; }
}
ViewModel
public class MyDate
{
public DateTime CalcDate { get; set; }
}
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谁设置
MyCollection
? 它不提供更改通知,因此绑定不知道它已更改。 更改为:或者更好的是,将其设为只读:
Who sets
MyCollection
? It is not providing change notification, so the binding doesn't know that it has been changed. Change to:Or, even better, make it read-only:
查看 Visual Studio 输出窗口,它将显示您可能遇到的任何数据绑定错误,这可能有助于您解决问题。
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.