翻譯|使用教程|編輯:龔雪|2024-06-04 10:13:18.600|閱讀 147 次
概述:本文將為大家演示如何將DevExpress GridControl添加到項(xiàng)目中,并將控件綁定到數(shù)據(jù)庫(kù),歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress WPF擁有120+個(gè)控件和庫(kù),將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過(guò)DevExpress WPF能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來(lái)新一代支持觸摸的解決方案。 無(wú)論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過(guò)DevExpress WPF控件來(lái)實(shí)現(xiàn)。
本教程演示了如何將GridControl添加到項(xiàng)目中,并將控件綁定到數(shù)據(jù)庫(kù):
DevExpress技術(shù)交流群10:532598169 歡迎一起進(jìn)群討論
1. 使用DevExpress Template Gallery(模板庫(kù))來(lái)創(chuàng)建一個(gè)新的Blank MVVM Application(空白MVVM應(yīng)用程序),這個(gè)項(xiàng)目模版包含了一個(gè)樣板View Model類,并設(shè)置MainView的數(shù)據(jù)上下文:
2. 將項(xiàng)目連接到本地?cái)?shù)據(jù)庫(kù),示例如下:。
3. 將GridControl工具箱項(xiàng)添加到MainView:
如果您的項(xiàng)目沒(méi)有DevExpress.Wpf.Grid.Core.v23.2引用,Visual Studio將顯示以下消息:
此消息通知您已添加所需的引用,并要求再次添加控件。
提示:如果您從NuGet源而不是從統(tǒng)一組件安裝程序中獲取DevExpress產(chǎn)品,則工具箱中不包含DevExpress控件,除非您添加相應(yīng)的NuGet包。
跳轉(zhuǎn)到Tools | NuGet Package Manager | Manage NuGet Packages for Solution,然后添加DevExpress.Wpf.Grid NuGet包。
4. 選擇GridControl然后調(diào)用它的Quick Actions(快捷操作)菜單,單擊Bind to a Data Source來(lái)啟動(dòng)Items Source Wizard(項(xiàng)目源向?qū)В?
5. 選擇一個(gè)數(shù)據(jù)源:
選擇要在GridControl中顯示的表:
選擇Simple Binding模型。
確保啟用了CRUD選項(xiàng):
選擇View Model選項(xiàng),在View Model中生成數(shù)據(jù)綁定代碼。確認(rèn)MainViewModel類被選擇為View Model:
6. Items Source Wizard(項(xiàng)目源向?qū)В┥梢韵麓a:
MainView.xaml
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding ItemsSource}" RestoreStateKeyFieldName="OrderId" RestoreStateOnSourceChange="True"> <dxg:GridControl.TotalSummary> <dxg:GridSummaryItem Alignment="Right" SummaryType="Count"/> </dxg:GridControl.TotalSummary> <dxg:GridControl.InputBindings> <KeyBinding Command="{Binding View.Commands.DeleteFocusedRow, ElementName=grid}" Key="Delete"/> </dxg:GridControl.InputBindings> <dxg:GridControl.View> <dxg:TableView NewItemRowPosition="Top" ShowUpdateRowButtons="OnCellEditorOpen" ValidateRowCommand="{Binding ValidateRowCommand}" ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}" DataSourceRefreshCommand="{Binding DataSourceRefreshCommand}" ShowFixedTotalSummary="True"/> </dxg:GridControl.View> <dxg:GridColumn FieldName="OrderId" IsSmart="True" ReadOnly="True"/> <dxg:GridColumn FieldName="CustomerId" IsSmart="True"/> <dxg:GridColumn FieldName="EmployeeId" IsSmart="True"/> <dxg:GridColumn FieldName="OrderDate" IsSmart="True"/> <dxg:GridColumn FieldName="RequiredDate" IsSmart="True"/> <dxg:GridColumn FieldName="ShippedDate" IsSmart="True"/> <dxg:GridColumn FieldName="ShipVia" IsSmart="True"/> <dxg:GridColumn FieldName="Freight" IsSmart="True"/> <dxg:GridColumn FieldName="ShipName" IsSmart="True"/> <dxg:GridColumn FieldName="ShipAddress" IsSmart="True"/> <dxg:GridColumn FieldName="ShipCity" IsSmart="True"/> <dxg:GridColumn FieldName="ShipRegion" IsSmart="True"/> <dxg:GridColumn FieldName="ShipPostalCode" IsSmart="True"/> <dxg:GridColumn FieldName="ShipCountry" IsSmart="True"/> </dxg:GridControl>
MainViewModel.cs
using DevExpress.Mvvm; using System; using WPF_DataGrid_GetStarted.Models; using DevExpress.Mvvm.DataAnnotations; using System.Linq; using System.Collections.Generic; using DevExpress.Mvvm.Xpf; namespace WPF_DataGrid_GetStarted.ViewModels { public class MainViewModel : ViewModelBase { NorthwindEntities _Context; IList<Order> _ItemsSource; public IList<Order> ItemsSource { get { if (_ItemsSource == null && !DevExpress.Mvvm.ViewModelBase.IsInDesignMode) { _Context = new NorthwindEntities(); _ItemsSource = _Context.Orders.ToList(); } return _ItemsSource; } } [Command] public void ValidateRow(RowValidationArgs args) { var item = (Order)args.Item; if (args.IsNewItem) _Context.Orders.Add(item); _Context.SaveChanges(); } [Command] public void ValidateRowDeletion(ValidateRowDeletionArgs args) { var item = (Order)args.Items.Single(); _Context.Orders.Remove(item); _Context.SaveChanges(); } [Command] public void DataSourceRefresh(DataSourceRefreshArgs args) { _ItemsSource = null; _Context = null; RaisePropertyChanged(nameof(ItemsSource)); } } }
MainViewModel.vb
Imports DevExpress.Mvvm Imports System Imports WPF_DataGrid_GetStarted.Models Imports DevExpress.Mvvm.DataAnnotations Imports System.Linq Imports System.Collections.Generic Imports DevExpress.Mvvm.Xpf Namespace WPF_DataGrid_GetStarted.ViewModels Public Class MainViewModel Inherits ViewModelBase Private _Context As NorthwindEntities Private _ItemsSource As IList(Of Order) Public ReadOnly Property ItemsSource As IList(Of Order) Get If _ItemsSource Is Nothing AndAlso Not DevExpress.Mvvm.ViewModelBase.IsInDesignMode Then _Context = New NorthwindEntities() _ItemsSource = _Context.Orders.ToList() End If Return _ItemsSource End Get End Property <Command> Public Sub ValidateRow(ByVal args As RowValidationArgs) Dim item = CType(args.Item, Order) If args.IsNewItem Then _Context.Orders.Add(item) _Context.SaveChanges() End Sub <Command> Public Sub ValidateRowDeletion(ByVal args As ValidateRowDeletionArgs) Dim item = CType(args.Items.Single(), Order) _Context.Orders.Remove(item) _Context.SaveChanges() End Sub <Command> Public Sub DataSourceRefresh(ByVal args As DataSourceRefreshArgs) _ItemsSource = Nothing _Context = Nothing RaisePropertyChanged(NameOf(ItemsSource)) End Sub End Class End Namespace
這段代碼啟用CRUD操作,為所有數(shù)據(jù)源字段生成列,并在固定的匯總面板中顯示總行數(shù)。
7. 運(yùn)行項(xiàng)目:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)