如何在 WPF 中动态创建标签和文本框列表? [英] How can I dynamically create a list of label and textboxes in WPF?

查看:58
本文介绍了如何在 WPF 中动态创建标签和文本框列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WPF 中创建标签和文本框列表,但失败了.我是一名 ASP.NET 开发人员,目前 XAML 体验让我有点不知所措...我面前有 Apress 的 Pro WPF 3.0 书,发现它的使用为零...

I am trying, and failing, to create a list of labels and text boxes in WPF. I am a ASP.NET developer and the XAML experience is slightly overwhelming me at the moment... I have Apress' Pro WPF 3.0 book infront of me and finding it zero use...

在我的流程结束时,我希望用户完成一些对该用户来说是动态的问题.我有一个对象数组,具有问题"和答案"的属性.

At the end of my process I want users to complete some questions that will be dynamic to that user. I have an Array of objects, with properties for a "Question" and "Answer".

我希望问题"显示为标签.

I want the "Question" to appear as the label.

我一直在研究 ListView 控件,但这似乎给了我一个我不感兴趣的 Excel 样式网格.

I've been looking at ListView controls, but this just seems to give me an Excel style grid which I am not interested in.

在 ASP.NET 世界中,我会使用带有两列的 GridView,一列带有标签,另一列带有文本框.在提交页面时,我会遍历网格视图中的项目以挑选出文本框的值,并将其关联回数组中的正确对象.

In the ASP.NET world I'd use a GridView, with two columns, one with a Label, the other with a TextBox. On submitting the page I'd loop through the items in the grid view to pick out the values of the textboxes, and associate back to the correct object in the Array.

有人可以指导或告诉我我应该在 WPF 中使用哪些控件吗?

Could someone please direct, or show me what controls I should be using in WPF?

额外信息;这是一个使用 .NET 4、Visual Studio 2010 的桌面 WPF 应用程序.

Extra info; It's a desktop WPF application using .NET 4, Visual Studio 2010.

干杯斯图

推荐答案

对于像这样简单的事情,绝对没有必要使用 DataGrid.使用基本的 ItemsControl 将完成您正在寻找的工作,而不会产生这种复杂控件的开销.这种方法也很容易定制,只需更改 ItemTemplate.

There's absolutely no need to use a DataGrid for something as simple as this. Using a basic ItemsControl will do what you're looking for without the overhead of such a complex control. This approach is also very easy to customize by just changing the ItemTemplate.

<ItemsControl ItemsSource="{Binding QuestionsToAnswer}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding QuestionText}"/>
                <TextBox Text="{Binding AnswerText}" Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于如何在 WPF 中动态创建标签和文本框列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆