1. 当ListBox放在Expander中时,为了要实现实时更新数据的效果,这里使用了

ObservableCollection类型来作为数据源,

初始的简单例子如下:只有一个ListBox

xaml文件

 <Window x:Class="ObservableCollectionAddRemoveDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox BorderBrush="Red" BorderThickness="2" HorizontalAlignment="Left" Height="Auto" Margin="37,32,0,0" VerticalAlignment="Top" Width="157" ItemsSource="{Binding}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" >
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Opacity" Value="0.5" />
<Setter Property="MaxHeight" Value="75" />
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Opacity" Value="1.0" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<ItemsControl HorizontalAlignment="Left" Height="auto" Margin="210,32,0,0" VerticalAlignment="Top" Width="157" ItemsSource="{Binding}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Opacity" Value="0.5" />
<Setter Property="MaxHeight" Value="75" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
<Button Content="Add" HorizontalAlignment="Left" Margin="398,65,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="Remove" HorizontalAlignment="Left" Margin="398,160,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_Remove"/> </Grid>
</Window>

后台文件

 using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace ObservableCollectionAddRemoveDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<String> list;
//public List<String> list;
public MainWindow()
{
InitializeComponent();
list = new ObservableCollection<string>() { "asda","12asdas","a22321","asda112323","xcvcvcxv","aasda","","asdasdasd"};
this.DataContext = list;
} private void Button_Click(object sender, RoutedEventArgs e)
{
int addNumber = new Random().Next();
list.Add(addNumber.ToString());
} private void Button_Click_Remove(object sender, RoutedEventArgs e)
{
if (list.Count > )
list.RemoveAt();
}
}
}

发现代码实现的很顺畅,无论是增删都能实时响应到界面中

2. 但当在ListBox外面套一个Expander时,问题就出现了,如下图:

在删除数据时,内容明显变少了,但属于删掉内容的位置确仍然保留在界面上!!!

解决的办法是:在Expander的 ContentPresenter外面套一个StackPanel,如下:

  <StackPanel>
<ContentPresenter x:Name="ExpanderContent" ContentSource="Content"/>
</StackPanel>

===========================================

当将Expander放在ListBox中时也有可能会出现类似的问题:https://www.dotblogs.com.tw/ouch1978/archive/2011/03/11/wpf-expander-in-listbox.aspx

最新文章

  1. jedisLock—redis分布式锁实现
  2. [Unity3D]NGUI用Sprite动画和屏幕自适应做游戏开始场景
  3. 数据结构--树(遍历,红黑,B树)
  4. puppet之自定义fact(转载)
  5. How to install Wordpress 4.0 on CentOS 7.0
  6. Vim配置文件(Vimrc)
  7. SQL in优化将In转化为联合查询
  8. C#使用Xamarin开发可移植移动应用目录
  9. LOJ #6041. 事情的相似度
  10. GitHub看板系统(Project)
  11. [转]angular官网 及 Ant Design of Angular
  12. ASP.NET页面之间传值的方式之Application(个人整理)
  13. error eslint@5.12.0: The engine &quot;node&quot; is incompatible with this module.
  14. 使用netlify-statuskit 进行系统业务状态报告
  15. Assetbundle管理与加载
  16. R语言-分组统计
  17. 用highcharts展现你的数据
  18. 怎么修改mysql主键(id)的值为自增
  19. Flask小demo---代码统计系统
  20. C#------?和??运算符的作用

热门文章

  1. Android Camera的使用(一) 读书笔记
  2. IIS部署网站常见问题
  3. tar.gz 解压
  4. 【MVC】输出HTML内容,不输出HTML标签
  5. tcp udp socket编程
  6. 洛谷P2179 [NOI2012]骑行川藏(拉格朗日乘数法)
  7. 「雅礼集训 2017 Day2」水箱
  8. linux 多线程之间信号传递
  9. Codeforces Global Round 2 部分题解
  10. Sweep Line