Search code examples
silverlightbindingsilverlight-4.0listboxitemsource

Is Binding ListBox to Collection<State> possible in Silverlight?


In silverlight is databinding to collection<*> allowed ? Because i did the below and nothing happens

<SilverlightToolkit:Accordion Name="ToolboxCategories" SelectionMode="ZeroOrMore">
                <SilverlightToolkit:Accordion.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </DataTemplate>
                </SilverlightToolkit:Accordion.ItemTemplate>
                <SilverlightToolkit:Accordion.ContentTemplate>
                    <DataTemplate>
                        <ListBox x:Name="CategoryControls" ItemsSource="{Binding States}" BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Canvas>
                                        <TextBlock Text="{Binding Name}"></TextBlock>
                                    </Canvas>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </DataTemplate>
                </SilverlightToolkit:Accordion.ContentTemplate>
            </SilverlightToolkit:Accordion>

Where Country is class that contains States property that is of type Collection<State>

Country

Public Class Country
{
  Public Collection<State> States;

  Public string Name{get;set;}
}

State

Public Class State
{
  Public string Name{get;set;}
}

Xaml.cs

List<Country> countries = DAL.GetCountries();

ToolboxCategories.ItemSource = countries;

note: I see that the accordian header shows the country name in each accordian header, but then Listbox is not databound with states.


Solution

  • Please make States as full property and this will work.

     public class Country
    {
        public Collection<State> States { get; set; }
    
        public string Name { get; set; }
    }
    

    Because when you set the Binding internally it looks for get_Property method of the object and you didn't make state as property that is why it is not showing. I hope this will help you to get rid of this issue.

    Cheers! Vinod