【发布时间】:2011-05-30 11:06:06
【问题描述】:
虽然在编写 Winforms 应用程序方面有些经验,但就最佳实践和设计模式而言,WPF 的“模糊性”仍然让我难以理解。
尽管在运行时填充了我的列表,但我的列表框显示为空。
我已按照this helpful article 的简单说明进行操作,但无济于事。我怀疑我错过了某种DataBind() 方法,我告诉列表框我已经完成了对基础列表的修改。
在我的 MainWindow.xaml 中,我有:
<ListBox ItemsSource="{Binding TopicList}" Height="177" HorizontalAlignment="Left" Margin="15,173,0,0" Name="listTopics" VerticalAlignment="Top" Width="236" Background="#0B000000">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在我的代码隐藏中,我有:
private void InitializeTopicList( MyDataContext context )
{
List<Topic> topicList = ( from topic in context.Topics select topic ).ToList();
foreach ( Topic topic in topicList )
{
CheckedListItem item = new CheckedListItem();
item.Name = topic.DisplayName;
item.ID = topic.ID;
TopicList.Add( item );
}
}
通过追踪,我知道其中填充了四个项目。
编辑
我已将TopicList 更改为ObservableCollection。还是不行。
public ObservableCollection<CheckedListItem> TopicList;
编辑#2
我做了两个有用的更改:
在 .xaml 文件中:
ListBox ItemsSource="{Binding}"
在我填充列表后的源代码中:
listTopics.DataContext = TopicList;
我得到一个列表,但是当我刷新复选框状态时,它不会自动更新复选框状态。我怀疑我的进一步阅读会解决这个问题。
【问题讨论】:
-
投反对票的人:这个问题究竟如何不显示研究工作、不清楚或没有用处?