2
votes

I'm working on Windows store app. Is there any way to programmatically create a listbox with datatemplate and then to bind data to datatemplate items?

I have created this listbox but now I need to create the same listbox programmatically because I need to create listboxes dynamically(count of listboxes is dynamic),

The second problem: I need to bind text to listbox datatemplate items. Does someone know something to recommend to me?

XAML:

<ListBox x:Name="lbTransitNow" ItemsSource="{Binding  MyDataBusStationsCurrent}" SelectionChanged="LbTransitNow_OnSelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <StackPanel>
                <Grid x:Name="gridTodayBtn">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="tbTransitStart" Grid.Column="0" Text="{Binding Lines_departure_date}" />
                    <TextBlock x:Name="tbTransitEnd" Grid.Column="1" Text="{Binding Lines_arrival_date}" />
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Code:

foreach (var routs in cpStationsrs.RoutesList)
{
    foreach (var lines in routs.LinesList)
    {
        foreach (var stops in lines.StopsList)
        {
            _myDataBusStationsChoosen.Add(new BindingData
                {
                    Lines_stops_nameSub = stops.Name,
                    Lines_stops_timeSub = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, stops.Time.Hour, stops.Time.Minute, 0).ToString("dd.MM.yyyy -- HH:mm"),
                    Lines_nameSub = lines.Name,
                    ...
                });
        }
    }
}

OnPropertyChanged("MyDataBusStationsChoosen");
1
You can create a DataTemplate programmatically in WPF, but not in WinRT... yet another missing feature in that not-quite-finished framework - Thomas Levesque
Why do you need to create it programmatically, btw ? - Thomas Levesque
Can't you create it in XAML as hidden, then change its visibility property to visible when you are ready for it? This would allow you to do all of your binding via XAML as usual, but not show the listbox until you are ready to. - philologon

1 Answers

3
votes

As far as I know, there is no way to create a DataTemplate programmatically in WinRT. However, if I understand your question correctly, you can reuse the same template for all listboxes... So you could just declare the template in the resources and reference it in each ListBox.

<Page.Resources>
        <DataTemplate x:Key="myItemTemplate">
            <StackPanel>
                <Grid x:Name="gridTodayBtn">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="tbTransitStart" Grid.Column="0" Text="{Binding Lines_departure_date}" />
                    <TextBlock x:Name="tbTransitEnd" Grid.Column="1" Text="{Binding Lines_arrival_date}" />
                </Grid>
            </StackPanel>
        </DataTemplate>
</Page.Resources>

Then, in code:

var listBox = new ListBox { ItemTemplate = (DataTemplate)Resources["myItemTemplate"] }