Wpf items控件不显示项目
自从我定期使用XAML以来,已经有一段时间了,我一直在为基础知识而挣扎 我试图在Wpf items控件不显示项目,wpf,xaml,itemscontrol,Wpf,Xaml,Itemscontrol,自从我定期使用XAML以来,已经有一段时间了,我一直在为基础知识而挣扎 我试图在ItemsControl中显示项目,如下所示: <DockPanel DockPanel.Dock="Left" Width="800"> <TextBlock DockPanel.Dock="Top" Text="{Binding ProfilePages.Count}"></TextBlock> <Grid> <ItemsC
ItemsControl
中显示项目,如下所示:
<DockPanel DockPanel.Dock="Left" Width="800">
<TextBlock DockPanel.Dock="Top" Text="{Binding ProfilePages.Count}"></TextBlock>
<Grid>
<ItemsControl ItemsSource="{Binding ProfilePages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="Hello World" Height="100" Width="200" Background="AliceBlue"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DockPanel>
ViewModel同样是基本的:
public class XtmProjectViewModel : NotifyingObject
{
private ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage> _profilePages;
public ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage> ProfilePages
{
get { return _profilePages; }
set
{
_profilePages = value;
RaisePropertyChanged(() => ProfilePages);
}
}
public ViewModelCollection<XtmSearchPageViewModel, XtmSearchPage> SearchPages { get; }
public XtmProjectViewModel(XtmProject model)
{
ProfilePages = new ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage>(model.ProfilePages, s => new XtmProfilePageViewModel(s));
SearchPages = new ViewModelCollection<XtmSearchPageViewModel, XtmSearchPage>(model.SearchPages, s => new XtmSearchPageViewModel(s));
ProfilePages.CollectionChanged += ProfilePages_CollectionChanged;
}
private void ProfilePages_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("Test");
RaisePropertyChanged(() => ProfilePages);
}
}
公共类XtmProjectViewModel:NotifyingObject
{
私有ViewModelCollection\u profilePages;
公共ViewModelCollection配置文件页
{
获取{return\u profilePages;}
设置
{
_profilePages=值;
RaisePropertyChanged(()=>ProfilePages);
}
}
公共ViewModelCollection搜索页面{get;}
公共XtmProjectViewModel(XtmProject模型)
{
ProfilePages=新的ViewModelCollection(model.ProfilePages,s=>新的XtmProfilePageViewModel);
SearchPages=新的ViewModelCollection(model.SearchPages,s=>新的XtmSearchPageViewModel);
ProfilePages.CollectionChanged+=ProfilePages\u CollectionChanged;
}
private void ProfilePages\u CollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
控制台写入线(“测试”);
RaisePropertyChanged(()=>ProfilePages);
}
}
ViewModelCollection
是一种自定义类型,可自动与基础模型集合同步。我已经在所有类型的场景中使用了多年,没有任何问题
然而,在视图中,这些项目没有出现,我得到了一个奇怪的行为,我无法解释:
- 绑定到ProfilePages.Count的文本块按预期工作,即显示的数字是列表中的项目数
- 没有绑定错误
-集合的ProfilePages
事件已正确触发CollectionChanged
- 另外,在
事件处理程序中为整个集合属性添加CollectionChanged
-事件不会更改行为RaisePropertyChanged
属性的get访问器在上一个sceanrio中被调用了两次(触发ProfilePages
)RaisePropertyChanged
- 当我在调试时编辑XAML时,有时项目会按预期显示在
中。但是,项目列表之后不会更新ItemsControl
ItemTemplate
,缺少CollectionChanged
事件,布局错误导致项目呈现不可见,等等,但没有成功)
如何解释这种行为?
如何解决这个问题?应OP的要求,将我的评论移至答案,15000我们来了;)
不知道是否将对象插入到ProfilePages中而不是UI线程上 想知道是否将对象插入到ProfilePages而不是UI线程上。我的第一个想法是
ViewModelCollection
不是IEnumerable
类型?但你说它有时会展示物品。您是否尝试过在XAML中使用CollectionViewSource
?您还可以创建临时集合,然后将其分配给您的ProfilePages
属性。@kenny,这是ist,谢谢@肯尼可以随时发布回复!我将删除我的并标记为已解决@马克,不客气。把它推到一个答案。