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