简体   繁体   English

如何为列表框的每个项目添加“删除”按钮并实现其功能?

[英]How to add a “Remove” button to each item of a ListBox and implement its functionality?

I need a "Remove" button for each item in the listbox of a UserControl. 我需要一个“删除”按钮,用于UserControl列表框中的每个项目。 I am trying to use a ListBox Template. 我正在尝试使用列表框模板。

<UserControl.Resources>
    <DataTemplate x:Key="ListBoxItemTemplate">
        <Grid>
            <TextBlock x:Name="TB" Height="23" Text="" VerticalAlignment="Top" />
            <Button Margin="500,0,0,0" Width="80" Click="Button_Click">remove</Button>
        </Grid>
    </DataTemplate>
</UserControl.Resources>

<ListBox x:Name="listbox" ItemTemplate="{StaticResource ListBoxItemTemplate}" SelectionChanged="ListBox_SelectionChanged"/>

How to make the text content of each item specified by the back-end code as shown below and how does the "remove" button remove its corresponding item? 如何使后端代码指定的每个项目的文本内容如下所示,并且“删除”按钮如何删除其对应的项目? Thanks. 谢谢。

listbox.Items.Add("111");
listbox.Items.Add("222");

Simpliest solution would be to use the ICommand system of WPF. 最简单的解决方案是使用WPF的ICommand系统。 Using this, you can bind your current item to the commands parameter, and use that in code beind. 使用此功能,可以将当前项目绑定到commands参数,并在代码beind中使用它。

(I am using window, not user control, but you can just change that.. (我使用的是窗口,而不是用户控件,但您可以更改它。

This is the WPF : 这是WPF

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" x:Name="Window" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="ListBoxItemTemplate">
            <Grid>
                <TextBlock Height="23" Text="{Binding}" VerticalAlignment="Top" />
                <Button Margin="500,0,0,0" Width="80" CommandParameter="{Binding}" Command="{Binding ElementName=Window, Path=OnClickCommand}">remove</Button>
            </Grid>
        </DataTemplate>
    </Window.Resources>

    <ListBox x:Name="listbox" ItemTemplate="{StaticResource ListBoxItemTemplate}" />

</Window>

{Binding} means to bind the current object the template is used on (in this instance, the string in my implementation. {Binding}表示绑定模板所使用的当前对象(在本例中为实现中的字符串)。

The OnClickCommand is what does the magic. OnClickCommand是什么。

C#: C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        listbox.Items.Add("asdf");
        listbox.Items.Add("fdsfsadf");
        listbox.Items.Add("dfsd");
        listbox.Items.Add("a sdfas");
        listbox.Items.Add("asdgas g");

        //This is the command that gets executed.
        OnClickCommand = new ActionCommand(x => listbox.Items.Remove(x));
    }

    public ICommand OnClickCommand { get; set; }
}

//This implementation of ICommand executes an action.
public class ActionCommand : ICommand
{
    private readonly Action<object> Action;
    private readonly Predicate<object> Predicate;

    public ActionCommand(Action<object> action) : this(action, x => true)
    {

    }
    public ActionCommand(Action<object> action, Predicate<object> predicate)
    {
        Action = action;
        Predicate = predicate;
    }

    public bool CanExecute(object parameter)
    {
        return Predicate(parameter);
    }
    public void Execute(object parameter)
    {
        Action(parameter);
    }

    //These lines are here to tie into WPF-s Can execute changed pipeline. Don't worry about it.
    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }
}

Note that it doesn't matter whether you use string or any other type of object, because {Binding} gets the current object that is in the ListBox.Items list 请注意,使用字符串还是任何其他类型的对象都没有关系,因为{Binding}获取ListBox.Items列表中的当前对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次单击按钮时,如何在列表框中添加项目? - How can i add item in listbox each time that an button was clicked? 如何在动态列表框中实现“添加新项”文本并更改其在添加项上的位置? - How can I implement “add new item” text in dynamic listbox and change its position on item added? 如何从一个列表框中删除项目并添加到另一个列表框? - how to Remove item from one Listbox and add into another Listbox? 如何在ViewModel中实现ListBox ScrollIntoView功能 - How to a implement ListBox ScrollIntoView functionality in a ViewModel 单击按钮时如何从listBox和ObservableCollection中删除自定义项 - How to remove a custom item from a listBox and ObservableCollection when click on a button C#:列表框:如何在每个项目的文本之外添加数据 - C#: Listbox: How to add data in addition to text for each item 如何为listBox中的每个项目添加menuStrip菜单? - How can i add a menuStrip menus for each item in a listBox? 如何单击一个按钮,该按钮将从ListBox中删除一个项目,而该列表中包含C#中ListBox的信息? - How do I click a button that will remove an item from a ListBox and the List that holds the information for the ListBox in C#? 如何在服务器端的每个按钮上添加具有单击功能的多个按钮? - How to add multiple buttons with click functionality on each button in server side? 如何将按钮绑定到列表框项目 - How to bind button to Listbox item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM