直接上代码:

第一步:创建测试类

    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. springsecurity:权限与异常处理

    权限即不同用户可以使用不同功能 实现前置: 在上一次登录与校验中,我们将authentication存入到SecurityContextHolder中,后续我们需要从FilterSecurityInt ...

  2. python_xecel

    移动并重命名工作簿 1 from pathlib import Path # 导入pathlib模块的path类 2 import time 3 4 # Press the green button ...

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

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

  4. 【RabbitMQ】05 通配符模式

    需要设定交换机模式为通配符模式 Topic 在绑定规则上采用通配描述实现动态绑定 创建通配符模式的生产者 package cn.dzz.topicQueue; import com.rabbitmq. ...

  5. Rust项目的代码组织

    学习一种编程语言时,常常优先关注在语言的语法和标准库上,希望能够尽快用上新语言来开发,我自己学习新的开发语言时也是这样. 不过,想用一种新的语言去开发实际的项目,或者自己做点小工具的话,除了语言本身之 ...

  6. 计算机领域:学术写作中的conducive的含义表示

    "Conducive" 的意思是"有助于"或"有益于".在学术和正式的写作中,"conducive" 常用于描述某种情况 ...

  7. 拜登开始在YouTube上打竞选广告了 —— 美国总统的竞选广告已经开始媒体投放了

    哈哈哈,老拜登,跑到YouTube上打广告了,这个画面真的太难想象,如果美国有"椰树"广告,估计拜登能弄个泳装上去打广告.有时不得不佩服西方搞的这种全民选举,最后搞的就和看小品似的 ...

  8. pytorch不像TensorFlow那样有专用的文件存储格式真的是不足吗?pytorch该如何处理大量小文件的读取呢?

    偶然发现前文: [转载] PyTorch下训练数据小文件转大文件读写(附有各种存储格式对比) 在谈论pytorch的文件读取问题,因为以前是搞TensorFlow的,后来由于编写效率和生态环境问题转为 ...

  9. 强化学习:连续控制问题中Actor-Critic算法的linear baseline

    最近在看连续控制问题,看到了一个Actor-Critic算法中手动扩展features和设置linear baseline的方法,这些方法源自论文:<Benchmarking Deep Rein ...

  10. 【转载】流形学习 (Manifold Learning) ——(学习笔记)

    第一篇:   摘抄自:https://zhuanlan.zhihu.com/p/54516805 从度量空间到拓扑空间 拓扑这门学科的一个方向涉及到去研究集合在"连续变形"下一些不 ...