翻譯|使用教程|編輯:龔雪|2022-01-19 10:21:53.237|閱讀 217 次
概述:本文主要介紹如何在應(yīng)用程序中使用未綁定的數(shù)據(jù)源,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
組件專為在編譯時沒有強類型數(shù)據(jù)集可用的非常規(guī)綁定場景而設(shè)計。
注意:UnboundDataSource 是數(shù)據(jù)感知控件和數(shù)據(jù)源之間的一層。
下圖說明了組件的基本功能。
是將DevExpress 數(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" ShowTotalSummary="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>
要將 映射到數(shù)據(jù),請使用 對象填充 集合,每個 對象標(biāo)識一個數(shù)據(jù)源字段。
下表列出了允許您將 對象與數(shù)據(jù)源字段相關(guān)聯(lián)的屬性。
下面的示例演示了 ,它表示具有不同類型的兩列的表,Numbers 列值使用就地 編輯器進行編輯。
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>
需要您手動處理數(shù)據(jù)操作,您可以通過處理以下事件來同步 GridControl 和數(shù)據(jù)源。
注意:
最初填充數(shù)據(jù)感知控件時,每次從數(shù)據(jù)源中提取值時都會觸發(fā) 事件。
例如,如果您將行數(shù)設(shè)置為 10,并且 集合包含 2 個 對象,則 事件將發(fā)生 20 次。
下面的示例演示鏈接到包含示例數(shù)據(jù)的 ViewModel 類的 。
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>
下圖展示了結(jié)果。
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強大互動功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過DevExpress WPF控件來實現(xiàn)。
DevExpress技術(shù)交流群5:742234706 歡迎一起進群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)