未綁定數(shù)據(jù)源
UnboundDataSource組件設(shè)計用于在編譯時沒有強類型數(shù)據(jù)集可用的非常規(guī)綁定場景。
提示:UnboundDataSource是數(shù)據(jù)感知控件和數(shù)據(jù)源之間的一層。
下圖展示了UnboundDataSource組件的基本功能。
初始化未綁定數(shù)據(jù)源
提示:項目源向?qū)г?a target="_blank">新的WPF XAML設(shè)計器中不可用。
項目源向?qū)菍evExpress數(shù)據(jù)感知控件綁定到任何受支持的數(shù)據(jù)源類型的最方便的方法。在本文中,以綁定數(shù)據(jù)網(wǎng)格為例。
1.通過單擊GridControl右上角的圖標(biāo)打開GridControl的Smart Tag面板選擇Items Source Wizard。
2.選擇“Unbound Data Source”選項并單擊 Next。
3.選擇“Simple Binding”選項并單擊 Next。
4.Row Count字段允許您指定GridControl將顯示的記錄數(shù)。
單擊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>
點擊復(fù)制
映射UnboundDataSource數(shù)據(jù)
要將UnboundDataSource映射到數(shù)據(jù),請使用UnboundDataSourceProperty對象填充UnboundDataSource.Properties集合,每個UnboundDataSourceProperty對象標(biāo)識一個數(shù)據(jù)源字段。
下表列出了允許您將UnboundDataSourceProperty對象與數(shù)據(jù)源字段關(guān)聯(lián)的屬性。
屬性 | 描述 |
---|---|
UnboundDataSourceProperty.Name | 指定由當(dāng)前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>
點擊復(fù)制
數(shù)據(jù)同步
UnboundDataSource要求您手動處理數(shù)據(jù)操作,可以通過處理以下事件來同步GridControl和數(shù)據(jù)源。
事件 | 描述 |
---|---|
UnboundDataSource.ValueNeeded | 每次從數(shù)據(jù)源中提取項時發(fā)生。 |
UnboundDataSource.ValuePushed | 每次將修改的數(shù)據(jù)推送到數(shù)據(jù)源時發(fā)生。 |
提示:當(dāng)初始填充數(shù)據(jù)感知控件時,UnboundDataSource.ValueNeeded事件在每次從數(shù)據(jù)源提取值時觸發(fā)。
例如,如果將行數(shù)設(shè)置為10,并且UnboundDataSource.Properties集合包含2個UnboundDataSourceProperty對象,則UnboundDataSource.ValueNeeded事件將發(fā)生20次。
下面的示例演示鏈接到包含示例數(shù)據(jù)的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; } }
點擊復(fù)制
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; } } }
點擊復(fù)制
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>
點擊復(fù)制
下圖展示了結(jié)果: