2

I'm new to WPF and databinding. I need to bind expander header to a List(Of Names) and expander content to a List(Of Services). I'm even more confused after reading MS tutorials on databindings (how and where to use staticResource, Path, etc.)

I have a

-------------
class Person 

name as string 
List servies as List (Of Services)

end class
--------------
class Service

name as string 
end class
----------------

In my main class Application.vb i have a list of Person objects

p1 as List(of Person)

I initialize all of them to dummy values. in Application.xaml, I have

<Expander Name="listBox4" VerticalAlignment="Top" 
 HorizontalAlignment="Left" Header="   {Binding}" Content="{Binding}" >

  <Expander.HeaderTemplate >

      <DataTemplate>
         <TextBlock Text="{Binding}"/>
      </DataTemplate>

   </Expander.HeaderTemplate> 

   <Expander.ContentTemplate>

      <DataTemplate >
        <ListBoxItem Content="{Binding}"/> 
      </DataTemplate>

    </Expander.ContentTemplate>                 

</Expander >

How do I bind header textblock to the person name and the inner listbox item to their services ?

1 Answer 1

3

Since you are working with a List of objects, you need to use an ItemsControl. Expanders can only deal with one DataContext, while ItemsControls were meant to work with Lists or Collections

Your code should look something like this:

<ItemsControl ItemsSource="{Binding PersonList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander Header="{Binding Name}">
                <ListBox ItemsSource="{Binding Services}" />
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

This creates a loop that says go through the PersonList and for each Person it generates an Expander with the Header equal to the Person's name and the Expanded Content equal to a ListBox which displays all of that Person's Services.

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

4 Comments

i can't refer to the variables in my Application.vb, how to i call them here, or is there a way to do this programmatically by only having the {Binding} in the XAML?
I'm not sure I understand what you mean. Whatever container holds the ItemsControl should have its DataContext set to your List of People. For example, you could put Me.MainWindow.DataContext = p1 in your MainWindow's loaded event. Once your DataContext is set, you can access any property of that DataContext, so if your DataContext is a Person object, you can bind to Person.Name using {Binding Name}, or Person.Services by {Binding Services}
hey, thanks! im just about getting the hand of what is DataContext is. I did what you said and overrode the toString methods and it worked! A followup, question, i put all this stuff in a <grid Name="grid1">. can we have multiple data contexts for this grid?
No, a Grid can only have one DataContext at a time. You can set different DataContext's for each of the Grid's child controls though, and you can reference properties outside of your DataContext through RelativeSource or ElementName bindings

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.