C# 如何将标签的ItemsControl绑定到ObservableCollection
我有一个ItemsControl,它的ItemsSource是一个可观察的集合。DataTemplate包含标签控件。我的目标是将这些标签的内容属性设置为ObservableCollection中的元素,但是现在,每个标签的内容都是空白的 值得注意的是,此ItemsControl嵌套在另一个父ItemsControl中,但让我显示:C# 如何将标签的ItemsControl绑定到ObservableCollection,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个ItemsControl,它的ItemsSource是一个可观察的集合。DataTemplate包含标签控件。我的目标是将这些标签的内容属性设置为ObservableCollection中的元素,但是现在,每个标签的内容都是空白的 值得注意的是,此ItemsControl嵌套在另一个父ItemsControl中,但让我显示: <ItemsControl ItemsSource={Binding StudentCollection}"> <ItemsControl.It
<ItemsControl ItemsSource={Binding StudentCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
// This is the ItemsControl that is not working properly with the Labels
<ItemsControl ItemsSource="{Binding StudentActivitiesCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Sport, UpdatedSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.Template>
</ItemsControl>
和我的工作视图模型:
private ObservableCollection<StudentActivities> studentActivitiesCollection;
public ObservableCollection<StudentActivities> StudentActivitiesCollection
{
get
{
if (studentActivitiesCollection == null)
studentActivitiesCollection = new ObservableCollection<StudentActivities>();
return studentActivitiesCollection;
}
}
这是我用来在ViewModel中填充ObservableCollection的方法:
private void PopulateStudentActivitiesCollection(ObservableCollection<Student> Students)
{
foreach (Student s in Students)
{
StudentActivitiesCollection.Add(new StudentActivities () { Sport = StudentSport });
}
}
}
改变
到
及
到
最后的更改不是必需的,但也不是必需的。我添加了一些代码供参考,我有StudentCollection的绑定,只是忘了复制它。我更新了我的帖子。对不起,我更新了我的帖子。请发布您的课程。什么是学生收藏?
private void PopulateStudentActivitiesCollection(ObservableCollection<Student> Students)
{
foreach (Student s in Students)
{
StudentActivitiesCollection.Add(new StudentActivities () { Sport = StudentSport });
}
}
}
<ItemsControl ItemsSource={StudentCollection}">
<ItemsControl ItemsSource={Binding StudentCollection}">
<Label Content="{Binding Sport, UpdatedSourceTrigger=PropertyChanged}"/>
<Label Content="{Binding Sport}"/>