Wpf 类似于XAML中的For循环

Wpf 类似于XAML中的For循环,wpf,xaml,Wpf,Xaml,我有一个资源字典,我想在其中为ComboBox提供一个通用的DataTemplate <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <DataTemplate DataType="{x:Type

我有一个资源字典,我想在其中为ComboBox提供一个通用的DataTemplate

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate DataType="{x:Type ComboBox}">
        <StackPanel Orientation="Horizontal">
            <!--Here I need to use something like For Loop-->
            <TextBlock Text=""></TextBlock>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

XAML中没有类似于
for
的东西,但是
ItemsControl
非常类似于
foreach
。与其设置
int
属性,不如创建一个
ObservableCollection
并向其中添加那么多对象,然后将
ItemsControl
绑定到集合属性


这还有一个额外的好处,即每个集合项都可以公开要绑定的属性,例如,如果您想在每个
TextBlock
中显示不同的文本,可以在集合项上放置一个属性,并将
TextBlock
绑定到该属性。

该DP的所有者是谁?您想在combobox中生成哪些列?我正在开发一个库存管理系统。我有所有的权利。有些组合框有两列,如组名和相应的效果。其中有些将有3列,有些将有1列。数据来自SQL Server。不,XAML中没有类似“循环”的东西。XAML是一种声明性语言,而不是命令式语言。没有“指令”,也没有可执行的XAML代码。您正在查找一个
项控件
。请发布一个你需要的屏幕截图,我可以告诉你在WPF中使用的正确方法。你考虑过标记扩展吗?我想你误解了我的问题。我想要一个多列组合框,并且我想要在资源字典中定义列。但问题是,所有组合框的列数都不相同。我想知道怎么做。我想你必须发布代码来展示你如何在组合框中创建列(以及如何绑定组合框的内容,如果适用)。ItemsControl仍然可以执行您想要的操作,但如果组合框是数据绑定的,则会更加困难。
<ComboBox x:Name="cbUnder" ItemsSource="{Binding GroupsAndCorrespondingEffects}" 
    IsEditable="True" SelectedItem="{Binding SelectedGroup, Mode=TwoWay}" 
    Text="{Binding InputValue, UpdateSourceTrigger=PropertyChanged}" TextSearch.TextPath="GroupName" 
    Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3">
    <ComboBox.Resources>
        <DataTemplate DataType="{x:Type vm:GroupAndCorrespondingEffect}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding GroupName}" Width="250">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
                                    <Setter Property="Foreground" Value="Blue" />
                                    <Setter Property="FontWeight" Value="Bold"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
                <TextBlock Text="{Binding CorrespondingEffect}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.Resources>
</ComboBox>