Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在运行时动态创建多个UI元素-C#WPF_C#_Wpf_Xaml_User Interface_Programmatically Created - Fatal编程技术网

在运行时动态创建多个UI元素-C#WPF

在运行时动态创建多个UI元素-C#WPF,c#,wpf,xaml,user-interface,programmatically-created,C#,Wpf,Xaml,User Interface,Programmatically Created,我对C#和WPF还比较陌生,但我开始构建一个应用程序,它应该具有列出一些细节的功能。现在看起来像 这些“项目”的数据(一个项目是一个边框中包含的多个标签(稍后我还想添加一张图片))是通过REST服务加载的,我不知道有多少项目会得到响应。现在我遇到的问题是,由于接收到的项目数量不断变化,无法在xaml中静态创建标签 我这里的问题是,如何以编程方式创建多个标签(以及边框和图像),在窗口中正确对齐标签,并对标签寻址以填充数据 非常感谢你的帮助 正如您在评论中指出的那样,列表框可能适合您的目的。一般的

我对C#和WPF还比较陌生,但我开始构建一个应用程序,它应该具有列出一些细节的功能。现在看起来像

这些“项目”的数据(一个项目是一个边框中包含的多个标签(稍后我还想添加一张图片))是通过REST服务加载的,我不知道有多少项目会得到响应。现在我遇到的问题是,由于接收到的项目数量不断变化,无法在xaml中静态创建标签

我这里的问题是,如何以编程方式创建多个标签(以及边框和图像),在窗口中正确对齐标签,并对标签寻址以填充数据


非常感谢你的帮助

正如您在评论中指出的那样,
列表框可能适合您的目的。一般的想法是,您希望通过
ItemTemplate
指定
ListBox
的格式,并在
ItemTemplate
中指定一个
DataTemplate
,其中包含支持API数据的必要控件,并指定绑定到从JSON映射的模型。XAML的一个不完整示例:

<ListBox x:Name="transactionsListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Black" BorderThickness=".5" Padding="1">
                <StackPanel>
                    <Label x:Name="id" Content="{Binding Path=Id}" />
                    <Label x:Name="item_id" Content="{Binding Path=Item_Id}" />
                    <Label x:Name="price" Content="{Binding Path=Price}" />
                    <Label x:Name="quantity" Content="{Binding Path=Quantity}" />
                    <Label x:Name="created" Content="{Binding Path=Created}" />
                    <Label x:Name="purchased" Content="{Binding Path=Purchased}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

上面绑定的Path=属性需要与您创建的用于存储REST调用中的事务的模型的属性名称相匹配。然后,当您拥有该模型类型列表的实例时,您的代码将执行以下操作:

List<Transaction> myTransactions = apiCall.response.mapToMyModel() // pseduocode
transactionsListBox.DataContext = myTransactions;
List myTransactions=apiCall.response.mapToMyModel()//psedoocode
TransactionListBox.DataContext=myTransactions;

要添加到Bobby的答案中,下面是一个使用
项控件的示例

<ItemsControl ItemsSource="{Binding SellTransactions}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" Content="{Binding created}"></Label>
                <Label Grid.Column="1" Content="{Binding id}"></Label>
                <Label Grid.Column="2" Content="{Binding item_id}"></Label>
                <Label Grid.Column="3" Content="{Binding price}"></Label>
                <Label Grid.Column="4" Content="{Binding purchased}"></Label>
                <Label Grid.Column="5" Content="{Binding quantity}"></Label>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我想你是在使用一个。还有一个不错的教程可能会有所帮助。我没有回答,因为有很多ItemsControl的示例用法。希望这将把您推向正确的方向。您能显示来自REST服务的Json响应对象吗?您将需要使用反射来创建控件。在窗口中正确对齐它们并用数据填充标签将取决于您从服务调用返回的结构。感谢您的帮助Yoink,我认为ListBox(稍后在您链接的教程中)或ItemsControl与我要查找的类似@鲍比:我看到了API的链接和我正在使用的响应。标签的数量不会变化;REST调用返回的“边界绑定项”的数量将是可变的。你赢了那就不需要思考了。我会发布一个答案,可能会给你指出正确的方向。是的,没错!我本来可以说得更好的,谢谢!我省略了您请求的图像部分,但它是XAML中定义的控件的简单扩展,作为StackPanel的子控件。此外,如果您希望更好地控制这些标签的显示,可以选择使用网格而不是StackPanel,并自定义网格的列以满足您的演示需要。
StackPanel
也应具有
Orientation=“Horizontal”
。非常感谢,效果非常好(感谢使用
Orientation=“Horizontal”的tipp)
!但是我还有一个问题,tlb.DataContext
tlb.ItemsSource
之间有什么区别,因为在这种情况下它只适用于
tlb.ItemsSource
DataContext
就像说“这是我们将要处理的对象类型”和
ItemsSource
所说的一样“以下是我希望重复的内容列表”非常感谢您的输入!我将使用类似于您在列表框中创建的网格
public class SellTransaction
{
    public long id { get; set; }
    public int item_id { get; set; }
    public int price { get; set; }
    public int quantity { get; set; }
    public DateTime created { get; set; }
    public DateTime purchased { get; set; }
}