WPF中Expander与ListBox(ItemsControl)嵌套中的问题

简介: 原文:WPF中Expander与ListBox(ItemsControl)嵌套中的问题1. 当ListBox放在Expander中时,为了要实现实时更新数据的效果,这里使用了    ObservableCollection类型来作为数据源,         初始的简单例子如下:只有一个List...
原文: 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

目录
相关文章
|
2月前
|
C# 容器
浅谈WPF之UniformGrid和ItemsControl
在日常开发中,有些布局非常具有规律性,比如相同的列宽,行高,均匀的排列等,为了简化开发,WPF提供了UniformGrid布局和ItemsControl容器,本文以一个简单的小例子,简述,如何在WPF开发中应用UniformGrid和ItemsControl实现均匀的布局,仅供学习分享使用,如有不足之处,还请指正。
64 0
|
8月前
|
C# 容器
WPF技术之Expander控件
WPF Expander控件是一个可折叠展开的容器,它允许用户在需要时展开或折叠其内容。它提供了一种在图形界面中组织和隐藏信息的方式。
272 0
|
8月前
|
C# 虚拟化 开发者
WPF技术之ListBox控件
WPF ListBox控件是一种用于显示和选择多个项的常用控件。它可以展示任意类型的数据,并允许用户通过鼠标或键盘进行选择操作
589 0
|
8月前
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
114 0
|
8月前
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
72 0
|
9月前
|
C# 容器
WPF框架下,窗体的嵌套显示
WPF框架下,窗体的嵌套显示
172 0
如何解决WPF中 ScrollViewer 内包含 TreeView 或者 ListBox 等控件时滚轮事件被劫持的问题
如何解决WPF中 ScrollViewer 内包含 TreeView 或者 ListBox 等控件时滚轮事件被劫持的问题
|
C#
创建一个显示所有预定义WPF颜色的ListBox
原文 Creating a ListBox that Shows All Predefined WPF Colors 在WPF中,您可以使用Colors类访问一系列预定义颜色,这些颜色定义为Colors类的静态属性。
948 0
|
C#
wpf listbox 选中项 上移下移
原文:wpf listbox 选中项 上移下移 private void MoveUp_Click(object sender, RoutedEventArgs e)         {             DataRowView rowView = this.
1142 0
|
C#
WPF里ItemsControl的分组实现
原文:WPF里ItemsControl的分组实现   我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid。WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox为例,他的分组效果图为:   以下为前台...
1068 0