翻譯|使用教程|編輯:龔雪|2024-07-16 10:51:03.317|閱讀 119 次
概述:本文主要介紹使用DevExpressWPF組件為項目添加網格控件并將其綁定到數據,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有著強大互動功能的XAML基礎應用程序,這些應用程序專注于當代客戶的需求和構建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產品,還是以數據為中心的商業智能產品,都能通過DevExpress WPF控件來實現。
本教程演示了如何將GridControl添加到項目中,并將控件綁定到數據庫:
DevExpress技術交流群10:532598169 歡迎一起進群討論
1. 使用DevExpress Template Gallery創建一個Blank MVVM Application,這個項目模板包含一個樣板View Model類,并將其設置為MainView的數據上下文:
2. 將項目連接到本地數據庫,示例如下:。
3. 將GridControl工具箱項添加到MainView:
如果您的項目沒有DevExpress.Wpf.Grid.Core.v24.1引用,Visual Studio將顯示以下消息:
此消息通知您已添加所需的引用,并要求再次添加控件。
提示:如果您從NuGet源而不是從統一組件安裝程序中獲取DevExpress產品,則工具箱中不包含DevExpress控件,除非添加相應的NuGet包。跳轉到Tools | NuGet Package Manager | Manage NuGet Packages for Solution,然后添加DevExpress.Wpf.Grid NuGet包。
4. 選擇GridControl然后調用Quick Actions菜單,點擊 Bind to a Data Source 來啟動項目源向導:
5. 選擇一個數據源:
選擇一個表格來顯示GridControl:
選擇Simple Binding模式。
確保啟用了CRUD選項:
選擇View Model選項,在View Model中生成數據綁定代碼,確認MainViewModel類被選擇為視圖模型:
6. 選擇項目源向導生成下面的代碼:
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>
MainView.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)); } } }
MainView.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操作,為所有數據源字段生成列,并在固定的匯總面板中顯示總行數。
7. 運行項目:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網