【发布时间】:2018-03-20 15:15:53
【问题描述】:
我有一个 ItemsControl,它的 ItemsSource 是 ObservableCollection。
每次选择按钮时,集合都会填充 Student 对象。
填充后的 ItemsControl 将如下所示
[------] 1
[------] 2
[------] 3
其中 [----] 是一个网格,数字是一个标签。 我实际上想将标签内容设置为我在另一个 ObservableCollection 中的项目。
所以假设这个 ObservableCollection 是 “Physics”、“Calculus”、“English”然后我希望标签在 ItemsSource 中读取为“Physics、“Calculus”和“English”
我的 XAML 类似于
<ItemsControl Grid.Row="1" Grid.ColumnSpan="3" ItemsSource="{Binding StudentCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Grid.Row="1" Grid.ColumnSpan="3" Margin="5, 0, 15, 0" Height="120">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox VerticalAlignment="Center"
HorizontalAlignment="Center"/>
<Label Grid.Row="1" Grid.Column="1"
HorizontalAlignment="Center"
Content =""
// This is where I want to set the values of the dynamically built Label to the items in the Collection.
FontFamily="Helvetica" FontWeight="Thin" FontSize="16"/>
【问题讨论】:
-
你是如何存储这些 ClassID 的?在集合中?或者它们是如何动态生成的!
-
动态生成,它们是它们在集合中所处位置的索引。
-
您能否将这部分代码添加到您的问题中?
-
您的绑定没有意义。标签应直接使用 ClassID,如果您只想在该控件中显示数字,请改用
TextBlock。<TextBlock Text="{Binding ClassID}"/>如果您想隐藏最后一个数字,请使用转换器并比较学生 classID == StudentCollection.Count - 1 例如。