WPF中Expander与ListBox(ItemsControl)嵌套中的问题
1. 当ListBox放在Expander中时,为了要实现实时更新数据的效果,这里使用了
ObservableCollection类型来作为数据源,
初始的简单例子如下:只有一个ListBox
xaml文件
1 <Window x:Class="ObservableCollectionAddRemoveDemo.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <ListBox BorderBrush="Red" BorderThickness="2" HorizontalAlignment="Left" Height="Auto" Margin="37,32,0,0" VerticalAlignment="Top" Width="157" ItemsSource="{Binding}"> 7 <ListBox.ItemContainerStyle> 8 <Style TargetType="ListBoxItem" > 9 <Setter Property="Opacity" Value="0.5" /> 10 <Setter Property="Opacity" Value="0.5" /> 11 <Setter Property="MaxHeight" Value="75" /> 12 <Setter Property="Background" Value="Green"/> 13 <Style.Triggers> 14 <Trigger Property="IsSelected" Value="True"> 15 <Setter Property="Opacity" Value="1.0" /> 16 </Trigger> 17 </Style.Triggers> 18 </Style> 19 </ListBox.ItemContainerStyle> 20 </ListBox> 21 <ItemsControl HorizontalAlignment="Left" Height="auto" Margin="210,32,0,0" VerticalAlignment="Top" Width="157" ItemsSource="{Binding}"> 22 <ItemsControl.ItemContainerStyle> 23 <Style TargetType="ContentPresenter"> 24 <Setter Property="Opacity" Value="0.5" /> 25 <Setter Property="Opacity" Value="0.5" /> 26 <Setter Property="MaxHeight" Value="75" /> 27 </Style> 28 </ItemsControl.ItemContainerStyle> 29 </ItemsControl> 30 <Button Content="Add" HorizontalAlignment="Left" Margin="398,65,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 31 <Button Content="Remove" HorizontalAlignment="Left" Margin="398,160,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_Remove"/> 32 33 </Grid> 34 </Window>
View Code
后台文件
1 using System; 2 using System.Collections.Generic; 3 using System.Collections.ObjectModel; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Windows; 8 using System.Windows.Controls; 9 using System.Windows.Data; 10 using System.Windows.Documents; 11 using System.Windows.Input; 12 using System.Windows.Media; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Navigation; 15 using System.Windows.Shapes; 16 17 namespace ObservableCollectionAddRemoveDemo 18 { 19 /// <summary> 20 /// Interaction logic for MainWindow.xaml 21 /// </summary> 22 public partial class MainWindow : Window 23 { 24 public ObservableCollection<String> list; 25 //public List<String> list; 26 public MainWindow() 27 { 28 InitializeComponent(); 29 list = new ObservableCollection<string>() { "asda","12asdas","a22321","asda112323","xcvcvcxv","aasda","123123","asdasdasd"}; 30 this.DataContext = list; 31 } 32 33 private void Button_Click(object sender, RoutedEventArgs e) 34 { 35 int addNumber = new Random().Next(999); 36 list.Add(addNumber.ToString()); 37 } 38 39 40 private void Button_Click_Remove(object sender, RoutedEventArgs e) 41 { 42 if (list.Count > 0) 43 list.RemoveAt(0); 44 } 45 } 46 }
View Code
发现代码实现的很顺畅,无论是增删都能实时响应到界面中
2. 但当在ListBox外面套一个Expander时,问题就出现了,如下图:
在删除数据时,内容明显变少了,但属于删掉内容的位置确仍然保留在界面上!!!
解决的办法是:在Expander的 ContentPresenter外面套一个StackPanel,如下:
1 <StackPanel> 2 <ContentPresenter x:Name="ExpanderContent" ContentSource="Content"/> 3 </StackPanel>
View Code
===========================================
当将Expander放在ListBox中时也有可能会出现类似的问题:https://www.dotblogs.com.tw/ouch1978/archive/2011/03/11/wpf-expander-in-listbox.aspx
WPF中Expander与ListBox(ItemsControl)嵌套中的问题相关推荐
- 【WPF】拖拽ListBox中的Item
原文:[WPF]拖拽ListBox中的Item 整理了两个关于WPF拖拽ListBox中的Item的功能.项目地址 https://github.com/Guxin233/WPF-DragItemIn ...
- 精通 WPF UI Virtualization (提升 OEA 框架中 TreeGrid 控件的性能)
精通 WPF UI Virtualization (提升 OEA 框架中 TreeGrid 控件的性能) 原文:精通 WPF UI Virtualization (提升 OEA 框架中 TreeGri ...
- R语言嵌套的ifelse语距:将一条If语句放在另一条If语句中,该语句作为嵌套的If else调用。If else语句允许我们根据表达式结果(TRUE或FALSE)打印不同的语句,执行不同的语句块
R语言嵌套的ifelse语句:将一条If语句放在另一条If语句中,该语句作为嵌套的If else调用.If else语句允许我们根据表达式结果(TRUE或FALSE)打印不同的语句,执行不同的语句块 ...
- SQL Server误区30日谈-Day26-SQL Server中存在真正的“事务嵌套”
本系列文章是我在sqlskill.com的PAUL的博客看到的,很多误区都比较具有典型性和代表性,原文来自T-SQL Tuesday #11: Misconceptions about.... EVE ...
- c#(winform)中ComboBox和ListBox添加项完全解决
c#(winform)中ComboBox和ListBox添加项完全解决 c#(winform)中ComboBox和ListBox添加项完全解决 刚开始用.net 的winform开发,发现好些控件都很 ...
- 减少代码中该死的 if else 嵌套
来源:http://www.apkbus.com/blog-970703-78964.html 写在前面 不知大家有没遇到过像"横放着的金字塔"一样的if else嵌套: if ( ...
- html5 webDatabase 存储中sql语句执行可嵌套使用
html5 webDatabase 存储中sql语句执行可嵌套使用,代码如下: var data = window.openDatabase("stu","1.0&quo ...
- WPF:使用Json.NET在TreeView中树形显示JSON数据
原文 WPF:使用Json.NET在TreeView中树形显示JSON数据 据 读者可以参考这个开源的可以树形显示XML和JSON的工具: Mgen Object 603:XML/JSON树形显示小工 ...
- php for嵌套循环_PHP中的for循环怎样嵌套
本篇文章主要介绍 PHP中的for循环怎样嵌套,感兴趣的朋友参考下,希望对大家有所帮助. for循环的执行原理: for循环的参数有(初始值:判断条件:更新循环变量表达式) 三者均不是必须的,若三者不 ...
最新文章
- R3Injector实例里的records map的填充逻辑
- 某中学要对学校运动会进行计算机管理,2020年新编高职入学考试适应性试卷信息技术试卷定稿名师精品资料....
- 《零基础》MySQL 数据类型(八)
- 人工智能数学基础-python数值计算实战
- 一款无惧“魔改”的设计系统开源了,已支持字节跳动4000多个项目
- 卓有成效的程序员读书笔记
- CentOS6和CentOS7进入单用户模式重置root密码
- 2021年高考成绩查询徐水,2021年4月河北保定徐水区自考成绩5月18日公布
- 计算机三级考点2:管理和运营宽带城域网的关键技术
- oracle 统计同比 环比,。oracle 同期 同比 环比
- webpack中loader和plugin的区别
- 华为服务器故障灯不开机_华为手机开不了机指示灯亮,怎么办
- uniapp editor富文本编辑器,h5富文本编辑器封装成插件
- 固态硬盘为啥这么快?带你了解固态硬盘的“秘密”……
- SpringCloud Day12---SpringCloud Alibaba Sentinel 服务熔断与限流
- 每日新闻丨​百度地图发布语音定制功能​;Windows 10高CPU使用率问题已解
- 小米笔记本装linux教程视频教程,重新安装系统,小编教你小米笔记本怎么重装win10系统...
- 15个权威的PLC学习资料下载地址 (西门子、三菱、欧姆龙)
- esp8266之Arduino的mqtt客户端远程继电器控制,断电重连、断网重连,断mqtt服务器重连
- 运行出错 runtime error