4

I have a list of objects (a custom class) that I want to display inside a ListBox, with each object drawing inside a custom User Control. Imagine a list of contacts (with custom Contact class) that should show up as a list of ContactUserControls (the XAML designed to present a Contact)

I know how to databind a list of Contact objects to a ListBox. I can databind a single Contact to a single ContactUserControl. I'm trying to understand the pattern/implementation of a databound list of objects that uses my custom UserControl to draw each object.

Do I bind the ListBox to my list of Contact objects, and (inside the Contact class) set up a connection to the ContactUserControl ("This is how you draw")? Do I bind the ListBox to a list of ContactUserControls, and bind each User Control to one of theses Contact objects before they go into the list? If so, do I have to do it manually via "ForEach" binding, or is there a "semi-magical" way in which it can be done purely via XAML?

Ideally, everything is correctly databound. Thanks! Not expecting somebody to present a turnkey solution of the entire thing, pointers to the applicable pattern/tutorials would be a great start.

3
  • Liked your question and that's the beauty of the XAML to tweak the way you want. eagerly waiting for someone's response with better solution/method. Commented Jan 9, 2017 at 1:07
  • I'm almost certain there's an elegant XAML-only solution. But I'm way to new to WPF to know where to start. Commented Jan 9, 2017 at 1:08
  • even I am not well-versed with XAML, but this can happen. 1) rather than using the the Listbox ,use custom control for a single contact. 2) repeat the contact custom control multiple times using for each loop or like inside the grid or datagrid Commented Jan 9, 2017 at 1:10

1 Answer 1

4

You can use <ListBox.ItemTemplate>. Something like this:

<ListBox ItemsSource="{Binding contacts}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:ContactUserControls DataContext="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

See https://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx the section about To format items in a ListBox or see https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate(v=vs.110).aspx the Examples section

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

2 Comments

Note also that DataContext="{Binding}" is usually in indication for a flawed UserControl implementation. It indicates that the UserControl explicitly sets its DataContent, and that the DataContext must therefore be explicitly overwritten by the code the uses the control. Instead of that, a UserControl should inherit the DataContext from its parent control, which works automatically by default, i.e. when not set explicitly.

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.