簡體   English   中英

如何為列表框的每個項目添加“刪除”按鈕並實現其功能?

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

我需要一個“刪除”按鈕,用於UserControl列表框中的每個項目。 我正在嘗試使用列表框模板。

<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"/>

如何使后端代碼指定的每個項目的文本內容如下所示,並且“刪除”按鈕如何刪除其對應的項目? 謝謝。

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

最簡單的解決方案是使用WPF的ICommand系統。 使用此功能,可以將當前項目綁定到commands參數,並在代碼beind中使用它。

(我使用的是窗口,而不是用戶控件,但您可以更改它。

這是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}表示綁定模板所使用的當前對象(在本例中為實現中的字符串)。

OnClickCommand是什么。

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;
        }
    }
}

請注意,使用字符串還是任何其他類型的對象都沒有關系,因為{Binding}獲取ListBox.Items列表中的當前對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM