未绑定数据源
UnboundDataSource组件设计用于在编译时没有强类型数据集可用的非常规绑定场景。
提示:UnboundDataSource是数据感知控件和数据源之间的一层。
下图展示了UnboundDataSource组件的基本功能。
初始化未绑定数据源
提示:项目源向导在中不可用。
项目源向导是将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="//schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxg="//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" TotalSummaryPosition="Bottom"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
点击复制
映射UnboundDataSource数据
要将UnboundDataSource映射到数据,请使用UnboundDataSourceProperty对象填充UnboundDataSource.Properties集合,每个UnboundDataSourceProperty对象标识一个数据源字段。
下表列出了允许您将UnboundDataSourceProperty对象与数据源字段关联的属性。
| 属性 | 描述 |
|---|---|
| UnboundDataSourceProperty.Name | 指定由当前UnboundDataSourceProperty表示的属性的名称。 |
| UnboundDataSourceProperty.DisplayName | 指定属性的显示名称。 |
| UnboundDataSourceProperty.PropertyType | 指定属性的类型 |
下面的示例演示了UnboundDataSource,它表示一个具有两列不同类型的表,Numbers列值使用 in-place SpinEdit编辑器编辑。
XAML:
<Window ...
xmlns:dx="//schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxg="//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" TotalSummaryPosition="Bottom"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
点击复制
数据同步
UnboundDataSource要求您手动处理数据操作,可以通过处理以下事件来同步GridControl和数据源。
| 事件 | 描述 |
|---|---|
| UnboundDataSource.ValueNeeded | 每次从数据源中提取项时发生。 |
| UnboundDataSource.ValuePushed | 每次将修改的数据推送到数据源时发生。 |
提示:当初始填充数据感知控件时,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="//schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxg="//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" TotalSummaryPosition="Bottom"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
点击复制
下图展示了结果:

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线