【解决方案】ItemsControl删除元素,但仍然显示它们
来源:互联网 发布:网络情缘陈星 编辑:程序博客网 时间:2024/06/07 07:43
例如一个选手机号的程序,有一系列手机号可选。有些手机号已经被人选走了,列表仍然显示它们,但多加一条删除线,如图。
如图,淡色的、被线划掉的就是已经被选走的。这样的功能该如何实现呢?
具体要求
外界可以把这种控件视为ListBox,加元素就是Items.Add,删元素就是Items.Remove。外界不应去关心如何“假删”。
此控件的应用范围应更为广泛,允许用模板来自定义被删除后的显示。
另外,出于个人爱好,此控件内部应尽量简单,少写代码;不重复发明微软专家已经发明的轮子。
实现
class RetainRemovedListBox : ListBox { public RetainRemovedListBox() { ((INotifyCollectionChanged)Items).CollectionChanged += Class1_CollectionChanged; } void Class1_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Remove) { Items.Insert(e.OldStartingIndex, e.OldItems[0]); EventHandler itemContainerGeneratorOnStatusChanged = null; itemContainerGeneratorOnStatusChanged = (sender1, e1) => { if (ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) { ItemContainerGenerator.StatusChanged -= itemContainerGeneratorOnStatusChanged; IsRemovedListBoxItem lbi = (IsRemovedListBoxItem)ItemContainerGenerator.ContainerFromItem(e.OldItems[0]); lbi.IsRemoved = true; lbi.IsEnabled = false; } }; ItemContainerGenerator.StatusChanged += itemContainerGeneratorOnStatusChanged; } } protected override DependencyObject GetContainerForItemOverride() { return new IsRemovedListBoxItem(); } protected override bool IsItemItsOwnContainerOverride(object item) { return item is IsRemovedListBoxItem; } } class IsRemovedListBoxItem : ListBoxItem { public static readonly DependencyProperty IsRemovedProperty = DependencyProperty.Register("IsRemoved", typeof(bool), typeof(IsRemovedListBoxItem), new FrameworkPropertyMetadata(false)); public bool IsRemoved { get { return (bool)GetValue(IsRemovedProperty); } set { SetValue(IsRemovedProperty, value); } } }
简单解释一下。所谓删除元素,其实删完以后又加了回去,但给它的Container做了记号,于是能够呈现“被删除”的外观。
GetContainerForItemOverride的父类实现是new ListBoxItem。
如果需要真正清除被删除的元素,可以在RetainRemovedListBox里面添加一个方法,对Items里每个元素调用ItemContainerGenerator.ContainerFromItem,检查IsRemoved,然后再删。当然了,Class1_CollectionChanged又会把这个元素添加回去。我相信,勤劳勇敢的中国人能自己解决好这个问题的。
用法
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow"> <DockPanel> <Button DockPanel.Dock="Top" Content="填充靓号" Padding="10,4" Click="Button_Click" /> <Button DockPanel.Dock="Bottom" Content="选择" Padding="10,4" Click="Button2_Click" /> <WpfApplication1:RetainRemovedListBox x:Name="myItemsControl"> <WpfApplication1:RetainRemovedListBox.ItemTemplate> <DataTemplate> <TextBlock Name="textBlock" Text="{Binding}"/> <DataTemplate.Triggers> <DataTrigger Binding="{Binding IsRemoved, RelativeSource={RelativeSource AncestorType=WpfApplication1:IsRemovedListBoxItem}}" Value="True"> <Setter TargetName="textBlock" Property="TextDecorations" Value="Strikethrough"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </WpfApplication1:RetainRemovedListBox.ItemTemplate> </WpfApplication1:RetainRemovedListBox> </DockPanel></Window>
private void Button2_Click(object sender, RoutedEventArgs e) { myItemsControl.Items.Remove(myItemsControl.SelectedItem); }
填充靓号的代码属技术专利,就不给了。:)
- 【解决方案】ItemsControl删除元素,但仍然显示它们
- DOM元素全屏显示解决方案
- jsp中设置了charset是UTF-8,但浏览器仍然显示乱码
- ArrayList删除元素并发异常ConcurrentModificationException解决方案
- DOM元素全屏显示解决方案(续)
- 如何把两个数组进行合并,并删除它们的相同元素
- 以正常的途径删除文件后,xcode仍然显示文件意外丢失报警 解决办法
- zeoslib:程序中删除数据表后,GetTableNames语句仍然显示该表存在的处理
- webdriver元素仍然没找到
- Windows Phone 8.1中数据显示控件基石------ItemsControl
- oracle数据库之删除数据文件[物理地删除一个数据文件但它的信息仍然记录在数据字典v$datafile中]
- 读《它们没大脑但它们有智能》
- 如何代表与JAXB空元素空值?(jaxb中引用@XmlNullPolicy仍然不能显示空节点)
- js数组的删除元素------jquery easyui tab组件未提供删除元素方法的解决方案
- "通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明" 的解决方案
- "通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明" 的解决方案
- 当创建公式或者视图列时,被删除的域仍然显示在域列表中
- 用Jquery怎么删除class相同 但id不同的元素?
- C 语言Shell 排序
- 短信发送--短信发送流程--应用层
- 数据库笔记六——存储过程
- ipv6地址解读---子网掩码
- Android中获取应用程序(包)的信息-----PackageManager的使用(一)
- 【解决方案】ItemsControl删除元素,但仍然显示它们
- linux内核中符号地址的获取
- 股票的亏与赚
- android start up flow
- Linux下Android环境搭建(附ndk、cdt、mat等)
- iBus拼音输入法导入搜狗词库
- 729 - The Hamming Distance Problem
- 算法导论 第18章 B树
- 终于明白了,为什么会有经济危机