Search code examples
wpfdatatemplateitemtemplateflipview

How to add flipviewitems dynamically with some content template defined


I have a FlipView in my Windows 8.1 app (RT) which has it's template defined as below:

<FlipView Grid.Row="3"
          Grid.ColumnSpan="2" x:Name="FlipView1" BorderBrush="Black"
          ItemsSource="{Binding ItemsCollection, RelativeSource={RelativeSource TemplatedParent}}">
            <FlipView.ItemTemplate>
                    <DataTemplate>
                          <ScrollViewer>
                                <Grid>
                                    <local:UserControlA x:Name="PART_UserControlA"/>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100" />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <local:UserControlB Grid.Column="1"
                                                            View="{Binding View}"
                                                            x:Name="PART_UserControlB"
                                                            ItemsSource="{Binding ItemsSourcePropertyOfAnItemInItemsCollection}"
                                                            ItemTemplate="{Binding TemplatePropertyOfAnItemInItemsCollection}" />
                                    </Grid>
                                </Grid>
                          </ScrollViewer>

                    </DataTemplate>
            </FlipView.ItemTemplate>
</FlipView>

Basically, this flip view is being used to display the calendar control. So, I am supposed to add items on selection changed event (to show the next/previous months/weeks calendar). How can I do that in code behind? The ItemsCollection contains the collection of say ObjectA. I have the new object ready (which is to be added to the ItemsSource collection of the flipview. And I am adding it to the ItemsSource of flip view as well. But, I think there is more to it than just adding the new item to the items source?


Solution

  • Ah, ignore my lack of WPF knowledge. It was as easy as making the ItemsCollection an ObservableCollection and defining the binding as TwoWay ItemsSource="{Binding ItemsCollection, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"