未綁定數據源
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="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" 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="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" 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="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" TotalSummaryPosition="Bottom"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
	點擊復制
下圖展示了結果:
 
 

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線