界面控件DevExpress WPF中文入门级教程 - 未绑定数据源
UnboundDataSource组件专为在编译时没有强类型数据集可用的非常规绑定场景而设计。
注意:UnboundDataSource 是数据感知控件和数据源之间的一层。
下图说明了UnboundDataSource组件的基本功能。

初始化未绑定数据源
Items Source Wizard是将DevExpress 数据感知控件绑定到任何支持的数据源类型的最方便的方法,本文以绑定数据网格为例。
1. 通过单击 GridControl 右上角的图标打开 GridControl 的Smart Tag,选择Items Source Wizard。

2. 选择“Unbound Data Source”,然后单击Next。

3. 选择“Simple Binding” ,然后单击Next。

4. Row Count 字段允许您指定 GridControl 将显示的记录数。

点击Finish后,将生成以下 XAML。
XAML
<Window ... xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"> <Window.Resources> <dx:UnboundDataSource x:Key="UnboundDataSource" Count="100"/> </Window.Resources> <Grid> <dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Data, Source={StaticResource UnboundDataSource}}"> <dxg:GridControl.View> <dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>
将 UnboundDataSource 映射到数据
要将 UnboundDataSource 映射到数据,请使用 UnboundDataSourceProperty对象填充 UnboundDataSource.Properties集合,每个 UnboundDataSourceProperty对象标识一个数据源字段。
下表列出了允许您将UnboundDataSourceProperty 对象与数据源字段相关联的属性。

下面的示例演示了 UnboundDataSource,它表示具有不同类型的两列的表,Numbers 列值使用就地 SpinEdit 编辑器进行编辑。
XAML
<Window ... xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"> <Window.Resources> <dx:UnboundDataSource x:Key="UnboundDataSource" Count="100"> <dx:UnboundDataSource.Properties> <dx:UnboundDataSourceProperty DisplayName="ID" Name="Numbers" PropertyType="{x:Type sys:Int32}"/> <!-- UnboundDataSourceProperty.DisplayName property specifies the default column header --> <dx:UnboundDataSourceProperty DisplayName="String Values" Name="Strings" PropertyType="{x:Type sys:String}"/> </dx:UnboundDataSource.Properties> </dx:UnboundDataSource> </Window.Resources> <Grid> <dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Data, Source={StaticResource UnboundDataSource}}"> <!-- UnboundSourceProperty.Name value is used to specify the field name --> <dxg:GridColumn FieldName="Numbers" Header="ID"> <dxg:GridColumn.EditSettings> <dxe:SpinEditSettings/> </dxg:GridColumn.EditSettings> </dxg:GridColumn> <dxg:GridControl.View> <dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>
同步数据
UnboundDataSource 需要您手动处理数据操作,您可以通过处理以下事件来同步 GridControl 和数据源。

注意:
最初填充数据感知控件时,每次从数据源中提取值时都会触发 UnboundDataSource.ValueNeeded 事件。
例如,如果您将行数设置为 10,并且 UnboundDataSource.Properties 集合包含 2 个 UnboundDataSourceProperty对象,则 UnboundDataSource.ValueNeeded 事件将发生 20 次。
下面的示例演示链接到包含示例数据的 ViewModel 类的 UnboundDataSource。
C#
public class ViewModel { //Each data cell is identified by a list index and key value. //The key is a string that specifies the column name. public List<Dictionary<string, object>> Data { get; set; } public ViewModel() { CreateList(); } //Populates the list with sample data void CreateList() { Data = new List<Dictionary<string, object>>(); //Creates 1000 rows for (int i = 0; i < 1000; i++) { Data.Add(CreateDictionary(i)); } } //Each dictionary object represents a data row. //Each dictionary item represents a cell value. It stores a string (column name) and a value (cell value) Dictionary<string,object> CreateDictionary(int i) { Dictionary<string, object> dict = new Dictionary<string, object>(); //Specifies the value in the "Strings" column dict.Add("Strings", "Value" + i.ToString()); //Specifies the value in the "Numbers" column dict.Add("Numbers", i); return dict; } }
C#
public partial class MainWindow : Window { public MainWindow() { vm = new ViewModel(); DataContext = vm; InitializeComponent(); } //Processes the pull operation private void UnboundDataSource_ValueNeeded(object sender, DevExpress.Data.UnboundSourceValueNeededEventArgs e) { var index = e.RowIndex; if(e.PropertyName == "Strings") { e.Value = vm.Data[index]["Strings"]; } if(e.PropertyName == "Numbers") { e.Value = vm.Data[index]["Numbers"]; } } //Processes the push operation private void UnboundDataSource_ValuePushed(object sender, DevExpress.Data.UnboundSourceValuePushedEventArgs e) { var index = e.RowIndex; if(e.PropertyName == "Strings") { vm.Data[index]["Strings"] = (string)e.Value; } if(e.PropertyName == "Numbers") { vm.Data[index]["Numbers"] = (int)e.Value; } } }
XAML
<Window ... xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"> <Window.Resources> <dx:UnboundDataSource x:Key="UnboundDataSource" Count="100" ValueNeeded="UnboundDataSource_ValueNeeded" ValuePushed="UnboundDataSource_ValuePushed"> <dx:UnboundDataSource.Properties> <dx:UnboundDataSourceProperty DisplayName="ID" Name="Numbers" PropertyType="{x:Type sys:Int32}"/> <dx:UnboundDataSourceProperty DisplayName="String Values" Name="Strings" PropertyType="{x:Type sys:String}"/> </dx:UnboundDataSource.Properties> </dx:UnboundDataSource> </Window.Resources> <Grid> <dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Data, Source={StaticResource UnboundDataSource}}"> <dxg:GridControl.View> <dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>
下图展示了结果。

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。
DevExpress技术交流群5:742234706 欢迎一起进群讨论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-01-20 透视网格的数据如何导出Excel?一招教你搞定
2021-01-20 DevExpress WinForms帮助文档:表单控件 - Tabbed Form
2020-01-20 Web UI开发神器—Kendo UI for jQuery数据管理之网格排序