blawford
blawford

Reputation: 465

MenuFlyoutItem Get Parent

I have a Flyout attached to a ListView Item, simplified as follows:

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel IsHoldingEnabled="True" Holding="ListView_Holding" >
            <FlyoutBase.AttachedFlyout>
                <MenuFlyout>
                    <MenuFlyoutItem Text="remove" Click="MenuFlyoutItem_Click"  />
                </MenuFlyout>
            </FlyoutBase.AttachedFlyout>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

private void ListView_Holding(object sender, HoldingRoutedEventArgs e)
{
    FrameworkElement senderElement = sender as FrameworkElement;
    FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);

    flyoutBase.ShowAt(senderElement);
}

private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{

}

How do I get the ListView item from within MenuFlyoutItem_Click that the MenuFlyout is 'attached' to? I have tried a few things but haven't been able to make it work.

I can post more code if required.

Upvotes: 4

Views: 1898

Answers (1)

Igor Ralic
Igor Ralic

Reputation: 15006

You can get the exact item by getting the DataContext. This will be the object from a collection you're binding to.

var datacontext = senderElement.DataContext;

Then you can get the ListViewItem from that DataContext.

ListViewItem item = this.NameOfYourList.ContainerFromItem(datacontext) as ListViewItem;

Upvotes: 2

Related Questions