【问题标题】:UWP remove item in listview by clicking a button in listviewUWP 通过单击列表视图中的按钮删除列表视图中的项目
【发布时间】:2017-07-11 00:09:05
【问题描述】:

当我单击列表视图行中的按钮时,我想从列表视图中删除一个项目。 listview 行由一个文本框和一个按钮组成:我需要获取文本框中文本的名称,因为它用于 xml 文件中,它会填充 listview 本身。

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>

【问题讨论】:

    标签: uwp uwp-xaml


    【解决方案1】:

    您可以绑定按钮的标签,例如Tag="{Binding Name}" 然后当您获得对象发送者时,您可以将其转换回按钮,例如Button button = (Button)sender;remove_Click 方法中然后从那个的 Tag 属性中读取名称

    【讨论】:

    • 这可行,但可能不是最好的解决方案。
    • 确实,这是我一看到问题就想到的,但如果有更好的方法建议,请随意更改已接受的答案 - 有时第一个想法不是最好的 -我很高兴承认这一点!
    • 使用我的代码它可以工作......我不知道是否存在更好的东西,@JustinXL 有什么建议吗?
    • @JustinXL 我不知道为什么,但昨天 stackoverflow 没有显示你的答案!
    • 如果 Justin XL 的答案效果更好,请随时将接受的答案更改为他们的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    相关资源
    最近更新 更多