3
votes

A Grid in my DataTemplate currently has this Flyout.

                    <Grid.ContextFlyout>
                        <MenuFlyout x:Name="AlbumFlyout">
                            <MenuFlyoutItem Icon="Shuffle" Text="Shuffle" />
                            <MenuFlyoutItem Icon="Delete" Text="Delete" />
                        </MenuFlyout>
                    </Grid.ContextFlyout>

And I also have a Flyout generated dynamically:

    public static MenuFlyout GetAddToPlaylistFlyout()
    {
        MenuFlyout flyout = new MenuFlyout();
        flyout.Items.Add(new MenuFlyoutItem()
        {
            Icon = new FontIcon() { Glyph = "\uEC4F" },
            Text = "Now Playing"
        });
        flyout.Items.Add(new MenuFlyoutSeparator());
        flyout.Items.Add(new MenuFlyoutItem()
        {
            Icon = new SymbolIcon(Symbol.Add),
            Text = "New Playlist"
        });
        foreach (var playlist in Settings.settings.Playlists)
        {
            var item = new MenuFlyoutItem()
            {
                Icon = new SymbolIcon(Symbol.Audio),
                Text = playlist.Name
            };
            flyout.Items.Add(item);
        }
        return flyout;
    }

It is dynamically generated because I need to reuse it a lot and some of its MenuFlyoutItem is generated from user's data.

How can I insert the code-generated Flyout right below the Shuffle above the Delete?

---Update---

This is part of my ListView definition.

<ListView
    x:Name="SongsListView"
    Grid.Row="1"
    AllowDrop="True"
    CanDrag="True"
    CanReorderItems="True"
    ContainerContentChanging="SongsListView_ContainerContentChanging"
    IsItemClickEnabled="True"
    ItemClick="SongsListView_ItemClick"
    ItemsSource="{Binding Songs, Mode=TwoWay}"
    ReorderMode="Enabled"
    SelectionMode="None">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="ContextFlyout" Value="{StaticResource ListViewItemContextFlyout}" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
1

1 Answers

3
votes

It looks like you have two fixed items, Shuffle and Delete.

You want to insert the newly generated MenuFlyout item between these two items. Try this code:

var newFlyout = GetAddToPlaylistFlyout();
foreach(var item in newFlyout.Items)
{
    AlbumFlyout.Items.Insert(AlbumFlyout.Items.Count - 1, item);
}

By the way, if your AlbumFlyout is in the DataTemplate and may not be available in Code-behind, you can try to put it in Page.Resources and then reference it with StaticResource.

Best regards.