How do I bind two different data collections into 2 different PanoramaItem listbox?

244 views Asked by At

I have AItems and BItems, want to bind to AListBox in a PanoramaItem and BListBox in another PanoramamItem.

I specified ItemsSource in each ListBox

<controls:PanoramaItem>
    <ListBox ItemsSource="{Binding AItems}" >
      ...
    </ListBox>
</controls:PanoramaItem>
<controls:PanoramaItem 
    <ListBox ItemsSource="{Binding BItems}" >
      ...
    </ListBox>
</controls:PanoramaItem>

How do I bind the data to each ListBox from code behind?

I have the Constructor method

public MainPage()
{
    InitializeComponent();
}

and page load method

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
}

Thank you.

1

There are 1 answers

0
ZombieSheep On

To bind the collections to controls in code-behind :

1) Give the listboxes names
2) in code behind, set <listboxName>.ItemsSource = <collection>

(You should make sure your collections are ObservableCollection<myType> to ensure that if the collection changes, this is reflected in the view.)

for example...

<controls:PanoramaItem>
    <ListBox Name="AItemBox" >
      ...
    </ListBox>
</controls:PanoramaItem>
<controls:PanoramaItem 
    <ListBox Name="BItemBox" >
      ...
    </ListBox>
</controls:PanoramaItem>

and then

private void BindMyControls()
{
    AItemBox.ItemSource = AItems;
    BItemBox.ItemSource = BItems;
}

Then you can call BindMyControls() from the most appropriate place, most likely once the collections have been populated.