Cyr
Cyr

Reputation: 501

UWP remove item in listview by clicking a button in listview

I would like to remove an item from the listview when I click a button in the listview row. The listview row is composed by a textbox and a button: I need to get the name of the text in the textbox becouse it is used in xml file, which populate the listview itself.

XAML

<ListView x:Name="listView_names" HorizontalAlignment="Left" Height="169" Margin="52,221,0,0" VerticalAlignment="Top" Width="336" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>

                <TextBlock Text="{Binding Name}"></TextBlock>
                <Button x:Name="removeBtn" Content="Remove" HorizontalAlignment="Left" Margin="0,30,0,0" VerticalAlignment="Top" Width="100" Background="#FF888888" Foreground="#FF292C33"  Click="remove_Click"/>

            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

listview with buttons

Upvotes: 0

Views: 1158

Answers (1)

RoguePlanetoid
RoguePlanetoid

Reputation: 4576

You could bind the Tag of the Button e.g. Tag="{Binding Name}" then when you get the object sender you can cast that back to a Button e.g. Button button = (Button)sender; in the remove_Click Method then read the name from the Tag property of that

Upvotes: 4

Related Questions