itemscontrol 中每个 itemsource 的 WPF 新列单元格

WPF new column cell for each itemsource in itemscontrol

我在 viewModel 中有一个 ObservableCollection<string>,我已将其设置为 ItemsControl 的项目源。

代码

<ItemsControl ItemsSource="{Binding NameofCollection}">
</ItemsControl>

它正常显示,1 列集合中的所有项目都出现在新行中。

现在,我想要一个新列,在当前列的旁边,它应该为所有项目显示一个固定的字符串(例如“yes”)。

因此,如果我在集合中有 5 个元素,则新列应包含 5 个单元格,每个单元格包含一个固定字符串“yes”。

自己努力实现。

你可以这样做:

            <ItemsControl ItemsSource="{Binding NameofCollection}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding}"/>
                            <TextBlock Grid.Column="1" Text="yes"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>