2

I need to create a dynamic forms in WPF. For example, say I need to allow the user to create a family tree with information about each of the people and each person will have infomation about their occupation.

class Person
{
    int age;
    int dateOfBirth;
    List<Job> jobList = new List<Job>();
}

class Job
{
    string title;
    int salary;
}

This program needs to be able to have fields that will allow data entry of all the members in the class, including multiple jobs. I would like the forms data to (possibly) coded in XAML, where if they click the button "Add Person" it will expand another person entry box to allow the user to add information about the person. This is the same as "Add Job" but the jobs are only added under that person.

(Note that the end result of this will be a tree data structure that contains all the people and their children)

How would I go about doing this in WPF using the MVVM pattern? Before I learned the MVVM pattern, I had used code behind and created dynamic view using c# to code the XAML view and added it dynamically as child elements. But I don't think that is the best way to do this since it seems too tedious.

I'm new to the MVVM pattern so some small code snippets of how I would do this (using databinding?) would be very helpful.

I made a quick example coded in XAML of how the form might look like:

enter image description here

6
  • 1
    This is very broad. Please start your project on your own, try to figure this out yourself, and if you end up having specific questions which you can qualify with your own code, then come back and post a question and someone will be happy to assist you, I'm sure. Commented Sep 8, 2014 at 15:52
  • 2
    @eddie_cat While a fully coded implementation would be too much for SO, the general concept here is actually pretty short. This is the whole point of collection controls with item templates in fact. Commented Sep 8, 2014 at 15:58
  • To clarify, I'm not asking for a fully coded implementation, I just need to know the specifics of what I would use to make what I described. Commented Sep 8, 2014 at 16:01
  • stackoverflow.com/questions/785927/… Commented Sep 8, 2014 at 16:02
  • @BradleyDotNET I didn't mark as a duplicate, just seemed relevant and possibly like it would have the same solution as this question, if not an exact dupe Commented Sep 8, 2014 at 16:04

1 Answer 1

7

This problem is solved in WPF using DataTemplates. In any place you need a "repeated" form, you will set up something like this:

<ItemsControl ItemsSource="{Binding Jobs}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <...>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

You use ListView to get a collection control, that will display a instance of the DataTemplate for every item in the bound collection. Inside of the DataTemplate, your DataContext is the bound item inside the collection, ie. an instance of the Job class.

If Jobs was an ObservableCollection<T> then the control would automatically update when items were added or removed from the bound collection.

Sign up to request clarification or add additional context in comments.

3 Comments

Perhaps an itemscontrol is more appropriate as I can't see a need to know the selected collection item?
@kidshaw Absolutley. Either would work, and my brain came up with ListView first. ItemsControl is likely a much better option though. Thanks for fixing my broken mind this morning!
Ha! Glad I helped you to function ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.