【问题标题】:How to assign controls in an ItemControl to the values in an ObservableCollection如何将 ItemControl 中的控件分配给 ObservableCollection 中的值
【发布时间】: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&lt;TextBlock Text="{Binding ClassID}"/&gt; 如果您想隐藏最后一个数字,请使用转换器并比较学生 classID == StudentCollection.Count - 1 例如。

标签: c# wpf xaml


【解决方案1】:

我使用 TextBlocks 而不是 Labels 但这应该是相似的...

<ItemsControl ItemsSource="{Binding StudentCollection}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
        <Grid>
            ....
            <TextBlock Text="{Binding SubListText}" />
        </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

还有你的 ViewModel:

public class ListViewModel {
    public ObservableCollection<Student> StudentCollection => ...;
}

public class Student {
    public ObservableCollection<Stuff> Stuff => ... ; //["Physics", ...]
    public string SubListText => string.Join(", ", Stuff);
}

或者您可以添加另一个 ItemsControl,例如:

<ItemsControl ItemsSource="{Binding StudentCollection}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
        <Grid>
            ....
            <ItemsControl ItemsSource="{Binding Stuff}">
              <ItemsControl.ItemTemplate>
                <DataTemplate>
                     <Grid>
                       <TextBlock Text="{Binding}" />
                     </Grid>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多