user2950509
user2950509

Reputation: 1057

How to retrieve Listview.Item from inner control?

So I have this relatively simple problem: I have the following ListView:

<ListView x:Name="lstView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="ItemGrid">
                <HyperlinkButton Click="HyperlinkButton_Click"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And I want to delete that particular item when the HyperlinkButton_Click event is fired. I've tried various things such as this:

PinOutDetails p = ((sender as HyperlinkButton).Parent as Grid).Parent as PinOutDetails;
lstView.Items.Remove(p);

But obviously, it doesn't work... So how can I get the Listview.Item (the PinOutDetails class) from inside one of it's child controls?

Upvotes: 0

Views: 54

Answers (1)

Shaun Sharples
Shaun Sharples

Reputation: 196

You last .Parent should be .DataContext as the DataContext of the Parent Grid should be your class you passed through.

PinOutDetails p = ((sender as HyperlinkButton).Parent as Grid).DataContext as PinOutDetails;
lstView.Items.Remove(p);

Unfortunately without more information such as how you are populating the list view in the first place, I can't provide more specific answers.

Upvotes: 2

Related Questions