直接上代码:

第一步:创建测试类

    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;

WPF中的ListBox怎么添加删除按钮并删除所在行的更多相关文章

  1. WPF中的ListBox实现按块显示元素的方法

    本文实例讲述了WPF中的ListBox实现按块显示元素的方法.分享给大家供大家参考,具体如下: 注意:需要设置ListBox的属性 ScrollViewer.HorizontalScrollBarVi ...

  2. WPF中修改ListBox项的样式病修改选中项的背景颜色

    最终效果: 1 <ListBox Name="cmb"> 2 <!--修改颜色--> 3 <ListBox.Resources> 4 <! ...

  3. WPF绑定的ListBox获取ListBoxItem及GoToState应用

    现公司项目中需要制作一个扇形菜单,菜单项是用ListBox重写Style实现的,其数据是绑定的.菜单的每一项都有Normal,MouseOver和Selected三种状态,这三种状态当然可以通过鼠标移 ...

  4. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

  5. WPF中ListBox的项ListBoxItem被选中的时候Background变化

    使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:

  6. WPF中ListBox /ListView如何改变选中条背景颜色

    适用ListBox /ListView WPF中LISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html

  7. WPF中ListBox的绑定

    WPF中列表式控件派生自ItemsControl类,继承了ItemsSource属性.ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值(所有可被迭代遍历的集合都 ...

  8. 转:WPF中ListBox的创建和多种绑定用法

    先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...

  9. WPF中ListBox滚动时的缓动效果

    原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...

  10. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

随机推荐

  1. Pycharm+pytest+allure打造高逼格的测试报告

    环境前置提示:allure是基于Java的一个程序,需要Java1.8的环境,没有安装需要去安装一下. 如果在cmd中能输入java,获取到命令信息则不管,否则需要配置系统变量: 路径:计算机> ...

  2. elasticsearch初步使用学习

    通过使用elasticsearch,我们可以加快搜索时间(直接使用SQL的模糊查询搜索耗时会比较久,而且elasticsearch的响应耗时与数据量关系不大) es主要用于存储,计算,搜索数据 依次部 ...

  3. 同时使用mp和mybatis,我的type-aliases-package失效了

    mybatis: #mapper配置文件 mapper-locations: classpath:mapper/*.xml type-aliases-package: com.sky.entity c ...

  4. 题解 CF741E Arpa’s abnormal DNA and Mehrdad’s deep interest

    CF741E Arpa's abnormal DNA and Mehrdad's deep interest 记 \(R_{i}\) 表示把 \(T\) 插入在 \(S\) 的第 \(i\) 位后组成 ...

  5. ios的idp/iep证书的生成方法,无苹果电脑

    在这个多端开发的年代,出现了很多优秀的开发框架,比如hbuilder和uniapp等等.我们可以使用这些框架来开发APP,假如我们要打包ios的app,则需要一个idp/iep证书. 那么这个证书是如 ...

  6. 【ActiveJdbc】04

    一.乐观锁 作者po的乐观锁思想: http://en.wikipedia.org/wiki/Optimistic_concurrency_control 维基百科,墙了看不到 作者要求表字段必须存在 ...

  7. 为什么要使用工业仿真软件? —— CAE(Computer Aided Engineering)工程设计中的计算机辅助工程

    CAE技术: 引自: https://baike.baidu.com/item/CAE技术/18884456?fr=ge_ala 引自: https://www.mscsoftware.com.cn/ ...

  8. 始智AI —— https://wisemodel.cn/ —— 试用

    清华大学的合资企业推出的服务: 始智AI -- https://wisemodel.cn/ 链接: 始智AI -- https://wisemodel.cn/ 和modelscope比相对简约,毕竟功 ...

  9. 【转载】 Python格式化字符串f-string概览

    版权声明:本文为CSDN博主「sunxb10」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/sunxb10/a ...

  10. (续 2 )在深度计算框架MindSpore中如何对不持续的计算进行处理——对数据集进行一定epoch数量的训练后,进行其他工作处理,再返回来接着进行一定epoch数量的训练——单步计算

    内容接前文: https://www.cnblogs.com/devilmaycry812839668/p/14988686.html https://www.cnblogs.com/devilmay ...