views:

52

answers:

2

Hi,

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.

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

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.

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

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

Cheers Stu

A: 

I agree with Scott that DataGrid is probably the way to go. Here are some decent turotials to get you started:
http://www.c-sharpcorner.com/UploadFile/mahesh/WpfDGP109272009111405AM/WpfDGP1.aspx
http://www.wpftutorial.net/DataGrid.html

KrisTrip
+5  A: 

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>
John Bowen
+1, I was thinking the same thing.
mdm20
You might want to put the TextBox in the second colume 'Column="1"' otherwise it will overlapping with the TextBlock. Correct me if im wrong but thats my experience of the grid layout control.
LnDCobra
I agree that an Items control is also a great solution! It always depends on what you specific scenario is. The thing I like about the grid, is you can always have the 'columns' line up automatically. In the example you provided... they do line up... but you're stuck with a fixed width to your first column. A datagrid will auto size to your largest question.
Scott
@LnDCobra - Right you are.@Scott - You definitely aren't stuck with a fixed column width. I put one here for simplicity but I could have set it to Auto with a SharedSizeGroup (and IsSharedSizeScope on the ItemsControl) to have them all use the width of the widest one just like DataGrid does.
John Bowen
@John... you're absolutely right. You can really do almost anything with an items control as you can with a DataGrid. Only reason I usually suggest a DataGrid is often, feature creep comes in as people want to do more and more things, and DataGrid has that extra stuff built in for you. I most often use an ItemsControl when my items are some custom UserControl, but when I add a Grid or stack panel to my items template, I find I'm generally re-creating a DataGrid. And at that point the overhead of a DataGrid isn't much of a factor anymore.
Scott
The great thing about WPF is you get options, and I think it's good for anyone reading this question to know they have options and to consider what their goal is before deciding which is the best option for them.
Scott
@Scott - The main reason I wouldn't recommend that to beginners is that using a DataGrid is great for tabular data, but when you start out with DataGrid as your default option, then everything starts looking like tabular data (the old hammer and nail problem) and you can end up losing out on a lot of the flexibility of WPF. Since ItemsControl is the base class of all of the list controls (including DataGrid) it's very easy to move up to a more complex control when you need to add specific functionality that's implemented by one of its derived types.
John Bowen
@John Bowen - thanks for the reply, this solved my problem!@All - Many thakns for all the additional comments, it's given good insight, and simplified it a great deal, thanks!
Strawberries and Cream