3

I have a DataTemplate for the ListBox Items. I would like that when the user holds a listbox item, a open Attached Flyout appears upon the item with some options. So when I hold an item of the listbox, in debug mode, I enter the method but it crashes telling me that there's no flyoutBase attached to that element, but there is... As I use a Custom DataTemplateSelector, the DataTemplate is written in the App.xaml as a resource. This is my DataTemplate for the ListBoxItem:

<DataTemplate x:Key="FollowerOuterTemplate">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" >
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Name="FollowersList">
                <FlyoutBase.AttachedFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="Delete" />
                        <MenuFlyoutItem Text="Refresh" />
                        <MenuFlyoutItem Text="Share" />
                    </MenuFlyout>
                </FlyoutBase.AttachedFlyout>
                <StackPanel Orientation="Vertical" Width="282">
                    <TextBlock Grid.Row="0" FontSize="33" Text="{Binding Pseudo}" Foreground="Gray" Height="46" Margin="0,0,-0.333,0"/>
                    <TextBlock Grid.Row="1" FontSize="20" Text="{Binding NomPrenom}" Foreground="#5bc5eb" Height="27" Margin="0,0,-0.333,0"/>
                    <TextBlock Grid.Row="2" FontSize="20" Text="Restez appuyer pour bloquer" Foreground="#BCC6CC" Height="27" Margin="0,0,-0.333,0"/>
                </StackPanel>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Width="113">
                    <Image Name="StatutContact"  Height="43" Source="/Ressources/Images/checkedTests2.png" Stretch="Fill" Margin="0,20,0,0" Width="44" HorizontalAlignment="Center"/>
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

And here is my called method:

private void FollowersList_Holding(object sender, HoldingRoutedEventArgs e)
    {
        try
        {
            FrameworkElement senderElement = sender as FrameworkElement;
            FrameworkElement element = sender as FrameworkElement;
            if (element == null) return;

            // If the menu was attached properly, we just need to call this handy method
            FlyoutBase.ShowAttachedFlyout(element);
        }
        catch (Exception ex)
        {

        }
    }

The exact error is : System.ArgumentException: The parameter is incorrect. ShowAttached was called on an element without an attached FlyoutBase

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • 1
    Is `FollowersList_Holding` attached to the `FollowersList` `StackPanel`? – Justin XL Dec 10 '14 at 00:46
  • Yes, As you can see in the xaml.... Or maybe I've missed something... – Hubert Solecki Dec 10 '14 at 13:11
  • Nope, I didn't see it in your xaml. – Justin XL Dec 10 '14 at 13:26
  • Oh yeah I'm sorry. The thing is that the DataTemplate is written in App.xaml as a resource and we cannot add event handler in the xaml in app.xaml. Actually, I enter in the **FollowersList_Holding** method so I believe it's attached... I've bind the event in the page constructor as follow: **FollowersList.Holding += FollowersList_Holding** – Hubert Solecki Dec 10 '14 at 13:28
  • 1
    You still can, the code will go to App.xaml.cs. But this is not the issue here. When I was testing I experienced similar app crashes too, however if I change to use a ListView the problem goes away. – Justin XL Dec 10 '14 at 13:37
  • 1
    Allright, I'll try to use use ListView instead of ListBox, I will keep you updated ! Thanks for the answers btw ! – Hubert Solecki Dec 10 '14 at 14:26

0 Answers0