WPF中,ListBox与数据间的绑定
2008-12-20 10:54
501 查看
1:新建一个WPF工程,并在XAML文件中添加一个ListBox控件,如下:
<Window x:Class="ListBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="800" Width="300">
<Grid>
<ListBox />
</Grid>
</Window>
2:在cs文件中添加一个类,并在其构造函数中获取系统当正在运行的进程的名称,代码如下:using System.Collections.Generic;
using System.Windows;
namespace ListBinding
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class Processes : List<string>
{
public Processes()
{
//在构造函数中取得系统中进程的名称并将其添加到类中
System.Diagnostics.Process[] pList = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in pList)
{
this.Add(p.ProcessName);
}
}
}
}
3:下面要进行控件与数据的绑定,修改后的XAML文件内容如下:<Window x:Class="ListBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:ListBinding"
Title="Window1" Height="800" Width="300">
<Window.Resources>
<src:Processes x:Key="p"/>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource p}"/>
</Grid>
</Window>
在上面的内容中,添加了一个命名空间src,其所代表的空间就是cs文件中定义的ListBinding;然后又以资源的方式向ListBox中添加了所有进程的名称。
由此就完成了ListBox与进程名称之间的绑定。
<Window x:Class="ListBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="800" Width="300">
<Grid>
<ListBox />
</Grid>
</Window>
2:在cs文件中添加一个类,并在其构造函数中获取系统当正在运行的进程的名称,代码如下:using System.Collections.Generic;
using System.Windows;
namespace ListBinding
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class Processes : List<string>
{
public Processes()
{
//在构造函数中取得系统中进程的名称并将其添加到类中
System.Diagnostics.Process[] pList = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in pList)
{
this.Add(p.ProcessName);
}
}
}
}
3:下面要进行控件与数据的绑定,修改后的XAML文件内容如下:<Window x:Class="ListBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:ListBinding"
Title="Window1" Height="800" Width="300">
<Window.Resources>
<src:Processes x:Key="p"/>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource p}"/>
</Grid>
</Window>
在上面的内容中,添加了一个命名空间src,其所代表的空间就是cs文件中定义的ListBinding;然后又以资源的方式向ListBox中添加了所有进程的名称。
由此就完成了ListBox与进程名称之间的绑定。
相关文章推荐
- WPF中关于ListBox绑定数据的问题
- WPF listbox异步绑定数据IsAsync=True
- WPF ListBox数据绑定
- 黑马程序员--wpf学习笔记五----说说ListBox\DataGrid与数据绑定的那点事
- Wpf ListBox数据绑定实例1--绑定字典集合
- WPF ListBox数据绑定
- WPF: ListBox绑定xml数据
- Wpf ListBox数据绑定实例1--绑定字典集合
- [导入]ListBox绑定数据,从左边移到右边的ListBox
- Silverlight中ListBox的数据绑定
- 入门级: WinForm 下的 ComboBox,ListBox 的使用 (二) 数据绑定
- WPF——数据绑定(一)什么是数据绑定
- winform中的ListBox和ComboBox绑定数据
- WPF 数据绑定 定制一个集合的视图 数据提供程序
- WPF模板数据绑定及事件模板绑定
- WPF中的数据绑定
- Wpf基础之数据绑定
- WPF的数据绑定(四)绑定到集合
- WPF数据绑定Binding(二)
- WPF PasswordBox.Password 的数据绑定