向ListBox绑定数据源时,如果数据量过大,可能会使得程序卡死,这是就需要一条一条的向ListBox的数据源中添加记录了,下面是个小Demo:

1.前台代码,就是一个ListBox控件

<Window x:Class="WpfApplication1.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 x:Name="listBox"></ListBox>
</Grid>
</Window>

2.后台代码,就是不断的向ListBox的绑定的数据源中添加记录,这里只是添加了一个描述数字,不断的调用委托添加数据,如果想停止添加数据时可以写停止条件。

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading; namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<string> _numberDescriptions; public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
} private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_numberDescriptions = new ObservableCollection<string>(); listBox.ItemsSource = _numberDescriptions; Dispatcher.BeginInvoke(DispatcherPriority.Background, new LoadNumberDelegate(LoadNumber), 1);
} private void LoadNumber(int number)
{
_numberDescriptions.Add("Number " + number);
Dispatcher.BeginInvoke(DispatcherPriority.Background, new LoadNumberDelegate(LoadNumber), ++number);
} private delegate void LoadNumberDelegate(int number);
}
}

3.效果图

异步方式向WPF ListBox控件中一条一条添加记录的更多相关文章

  1. MFC中listbox控件中各种属性的详解

     ListBox控件是Windows 窗体的一个空间,ListBox 控件显示一个项列表,用户可从中选择一项或多项.      如果项总数超出可以显示的项数,则自动向 ListBox 控件添加滚动条. ...

  2. WPF Image控件中的ImageSource与Bitmap的互相转换

    原文:WPF Image控件中的ImageSource与Bitmap的互相转换  1.从bitmap转换成ImageSource [DllImport("gdi32.dll", ...

  3. 对ListBox控件中的数据进行排序

    实现效果: 知识运用: ListBox控件的Sorted属性 //ListBox控件中的数据项是否按字母顺序排序 public bool Sorted{get;set;} 实现代码: private ...

  4. 选中ListBox控件中的全部项

    实现效果: 知识运用: ListBox控件的SelectedItems属性 //获取ListBox控件中被选中数据项的集合 public ListBox.SelectedObjectCollectio ...

  5. wpf 保存控件中的内容为图片格式

    黄色的是wpf控件的名称! //保存到特定路径            FileStream fs = new FileStream(@"C:\image.png", FileMod ...

  6. WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件

    之前做项目的时候需要实现这样一个功能.WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则 ...

  7. C# WinForm 中Console 重定向输出到ListBox控件中显示

                        {              VoidAction action =              {                  lstBox.Items. ...

  8. WPF 列表控件中的子控件上下文绑定

    <DataGrid Grid.ColumnSpan=" Height="Auto" SelectedItem="{Binding Path=SelectP ...

  9. WPF TextBox控件中文字实现垂直居中

    TextBox纵向长度比较长但文字字体比较小的时候,在输入时就会发现文字不是垂直居中的. 而使用中我们发现,TextBox虽然可以设置文字的水平对齐方式,但却没有相应的属性让我们来调节他的垂直对齐方式 ...

随机推荐

  1. 如何在Eclipse中查看Android API源码以及support包源码

    http://my.eoe.cn/futurexiong/archive/181.html 开发第三方Android应用的,大多数人应该还是Eclipse结合ADT来开发.那么大多数时候我们可能希望点 ...

  2. Sublime Text2 快捷键设置

    设置Tab两个空格: 点击 Preference -> Settings-User "tab_size":2, "translate_tabs_to_spaces& ...

  3. wordpress函数技巧

    1.Loop循环(成功) <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post(); ?> // t ...

  4. php 联系电话验证(手机和固话)

    $tel='要验证的联系电话'; $isMob="/^1[3-5,8]{1}[0-9]{9}$/"; $isTel="/^([0-9]{3,4}-)?[0-9]{7,8} ...

  5. DNA Sorting--hdu1379

    DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. Digital Roots(hdoj1013)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  7. ActionResult 之HttpGet HttpPost

    GET一般用于获取和查询数据. 当浏览器发送HTTP GET 请求的时候,会找到使用HttpGet限定的对应Action. POST 一般用于更新数据. 当Action上面没有限定的时候,浏览器发送任 ...

  8. SQL Server 内存使用量下降问题

    SQL server这个程序是非喜欢内存这东西的.所以它的内存使用量下降,一定是被别人给抢去了.这件事的后果就是SQL Server 变的 非常慢.怎么样才可以让这件事不太容易发生呢? ------- ...

  9. 如何让ios app支持32位和64位?

    将ios app转换为兼容32位和64位步骤:  1. 安装 Xcode 5.  2. 打开你的项目.Xcode会提示你更新你的项目,其中的警告和错误信息对于转换到64位相当重要.  3. 将你的项目 ...

  10. 常用的IO流

    常用的IO流 •根据处理数据类型的不同分为:字节流和字符流 •根据数据流向不同分为:输入流和输出流 字节流:字节流以字节(8bit)为单位,能处理所有类型的数据(如图片.avi等). 字节输入流:In ...