Silverlight - binding a listbox within a listbox t

2019-05-28 02:10发布

问题:

In silverlight, I'm trying to build a listbox of reports and I'm trying to show the parameters of the report in a listbox within the datatemplate of the outer reports listbox.

Here are the data classes:

public class Report
{
    public string Title { get; set; }
    public string Description  { get; set; }
    public List<ReportParameter> Parameters = new List<ReportParameter>();
}

public class ReportParameter
{
    public string Name { get; set; }
    public string ParameterType { get; set; }
    public bool Required { get; set; }
}

Here's the XAML I'm trying to use to do it:

<ListBox x:Name="lstReports">            
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border>
                    <StackPanel>
                        <TextBlock Text="{Binding Title}"/>                            
                        <ListBox ItemsSource="{Binding Parameters}"  Height="60" Width="60">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>                                 
                        </ListBox>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

The binding for the Title of the report works, but the inner listbox is empty for each of the reports.

The Parameters list is populated.

What am I doing wrong?

Thanks!

回答1:

Your "Parameters" list is a public field not a property, silverlight can only bind to properties and not fields. Try changing your class to this:

public class Report
{
    public Report()
    {
         Parameters = new List<ReportParameter>();
    }

    public string Title { get; set; }
    public string Description  { get; set; }
    public List<ReportParameter> Parameters { get; set; }
}

This should work the way you want it.