首页 > 其他分享 >WPF中的ListBox怎么添加删除按钮并删除所在行

WPF中的ListBox怎么添加删除按钮并删除所在行

时间:2024-10-09 17:03:05浏览次数:1  
标签:set Name 删除 get BeautifulGirl new WPF ListBox public

直接上代码:

第一步:创建测试类

    public class BeautifulGirl
    {
        public string Name { get; set; }
    }

第二步:创建viewmodel和数据源

    public class MainWindowVm
    {
        public ObservableCollection<BeautifulGirl> Girls { get; set; }

        public MainWindowVm()
        {
            Girls = new ObservableCollection<BeautifulGirl>
            {
                new BeautifulGirl
                {
                    Name ="刘亦菲",
                },
                new BeautifulGirl
                {
                    Name ="高圆圆",
                },
                new BeautifulGirl
                {
                    Name ="凤姐",
                }
            };
        }
    }

第三步:绑定数据和界面显示

 

<ListBox ItemsSource="{Binding Girls}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:BeautifulGirl}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

运行起来就会得到下面的结果:

 现在你想把里面的凤姐给删了,或者你要是喜欢凤姐,想任意删一个,怎么办

        <ListBox ItemsSource="{Binding Girls}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:BeautifulGirl}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"/>
                        <Button Content="X" Margin="10,0,0,0" Width="100"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

那就在我们的listbox里面给每一条后面都加一个删除按钮

第四步:写删除逻辑,一般都用command和viewmodel来互动,所以我们先创建一个command

public class CommandBase : ICommand
{
    public event EventHandler CanExecuteChanged;

    public Action<object> AcExecute { get; set; }

    public Func<object, bool> FcCanExecute { get; set; }

    public CommandBase(Action<object> execute, Func<object, bool> canExecute)
    {
        this.AcExecute = execute;
        this.FcCanExecute = canExecute;
    }
    public CommandBase(Action<object> execute)
    {
        this.AcExecute = execute;
        this.FcCanExecute = (o) =>
        {
            return true;
        };
    }
    public bool CanExecute(object parameter)
    {
        if (FcCanExecute != null)
        {
            return FcCanExecute(parameter);
        }
        return false;
    }
    public void Execute(object parameter)
    {
        AcExecute(parameter);
    }
}

怎么使用这个command

public class MainWindowVm
{
    public ObservableCollection<BeautifulGirl> Girls { get; set; }

    public CommandBase DelCommand { get; set; }

    public MainWindowVm()
    {
        Girls = new ObservableCollection<BeautifulGirl>
        {
            new BeautifulGirl
            {
                Name ="刘亦菲",
            },
            new BeautifulGirl
            {
                Name ="高圆圆",
            },
            new BeautifulGirl
            {
                Name ="凤姐",
            }
        };

        DelCommand = new CommandBase(DelAction);
    }

    private void DelAction(object parameter)
    {
        var girl = parameter as BeautifulGirl;
        if (girl != null)
        {
            Girls.Remove(girl);
        }
    }
}

在viewmodel里面创建一个command,和command对应的方法

前端绑定一下

<ListBox ItemsSource="{Binding Girls}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:BeautifulGirl}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
                <Button Content="X" Margin="10,0,0,0" Width="100"
                        Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.DelCommand}"
                        CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=DataContext}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

注意看里面的红色部分代码

最后运行一下

 删除多余的两个,只留下喜欢的

(本博客只是玩梗,没有人身攻击的意思)

 

项目github地址:bearhanQ/WPFFramework: Share some experience (github.com)

QQ技术交流群:332035933;

 

标签:set,Name,删除,get,BeautifulGirl,new,WPF,ListBox,public
From: https://www.cnblogs.com/lvpp13/p/18454644

相关文章

  • 如何从数码相机中恢复已删除的照片
    照片恢复是恢复已删除照片的最佳工具,带有恢复RAW照片的选项。在本文中,我们将解释如何检索已删除的照片。不仅适用于专业摄影师,而且对于像我们这样喜欢捕捉回忆的人来说,Moments相机是一种重要的数字设备。可悲的是,就像智能手机或任何其他电子设备一样,它们有时也会出现故障。......
  • 代码随想录算法训练营第四天|24. 两两交换链表中的节点、19.删除链表的倒数第N个节点
    24.两两交换链表中的节点力扣题目链接(opensnewwindow)给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。输入:head=[1,2,3,4]输出:[2,1,4,3]示例2:输入:head=[]输出:[]示例3:输入:head=[1]......
  • Android 8.1 桌面上的不能拖动卸载/删除,只能在后台系统—应用那里进行卸载
    —a/packages/apps/Launcher3/res/layout/drop_target_bar_horz.xml+++b/packages/apps/Launcher3/res/layout/drop_target_bar_horz.xml@@-37,6+37,7@@android:layout_height=“match_parent”android:layout_gravity=“center”android:gravity=“center”......
  • 如何删除或减少pagefile.sys?
    您是否曾在计算机上遇到过一个名为pagefile.sys的超大文件?许多Windows用户想知道pagefile.sys的用途以及如何在不影响系统性能的情况下有效地管理它。本文将帮助您了解pagefile.sys是什么、它为什么会变大以及如何安全地管理它的大小。什么是pagefile.sys?在Win......
  • Error:WPF项目中使用oxyplot,错误提示命名空间中不存在“Plot”名称
    在OxyPlot中,<oxy:PlotView>和<oxy:Plot>都是用来显示图表的控件,在WPF项目中使用oxyplot之前,先通过NuGet安装依赖包:OxyPlot.Wpf。<oxy:PlotView>和<oxy:Plot>使用示例:<oxy:PlotView>控件是一个视图控件,它绑定到一个PlotModel对象。这意味着你可以创建一个PlotModel实例,配置......
  • WPF MVVM第一篇-MVVM框架搭建
    1.创建view界面<Windowx:Class="WpfFramework.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schema......
  • WPF string format
    Text="{BindingStringFormat={}{0}items,Source={StaticResourcemainVM},Path=Cnt}"                //xaml<Windowx:Class="WpfApp17.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre......
  • WPF ListBoxItem Selected and background changed at the same time via ItemContain
    <Window.Resources><Stylex:Key="lbxItemContainerStyle"TargetType="ListBoxItem"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType=&quo......
  • 【VMware VCF】删除 SDDC Manager 映像管理中的集群映像。
    登录SDDCManagerUI,导航到生命周期管理->映像管理,这里显示了由SDDCManager映像管理的集群映像,这些映像可能是从现有vCenterServer集群中提取的,也可能是通过外部导入的映像。你可能会发现,这些列表中的映像只能被添加,无法对其进行删除,至少在WEBUI中是这样的。也许,VMwar......
  • WPF ListBox IsSynchronizedWithCurrentItem True ScrollIntoView via behavior CallM
    <ListBoxGrid.Column="0"ItemContainerStyle="{StaticResourcelbxItemContainerStyle}"ItemsSource="{BindingBooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"IsSynchronizedWith......