用舊電腦做網(wǎng)站推廣網(wǎng)站怎么制作
本教程講解了如何添加GridControl到wpf項(xiàng)目中并且綁定數(shù)據(jù)
原文地址Lesson 1 - Add a GridControl to a Project and Bind it to Data | WPF Controls | DevExpress Documentation
1、使用?DevExpress Template Gallery創(chuàng)建一個(gè)新的空白mvvm應(yīng)用程序,這個(gè)項(xiàng)目包括了一個(gè)視圖模型,設(shè)置此視圖模型作為MainView數(shù)據(jù)上下文
2、按照如下給此項(xiàng)目添加數(shù)據(jù)庫Blank .NET 6 App with the Northwind Database?.
3、在MainView中添加工具箱選項(xiàng)GridControl:
如果你的項(xiàng)目沒有DevExpress.Wpf.Grid.Core 引用,vs一定顯示如下信息:
此消息提示你必須添加控件引用。
如果你從Nuget訂閱devexpress,進(jìn)入工具,nuget包管理器,添加DevExpress.Wpf.Grid
4、在Quick Actions菜單中選擇GridControl,點(diǎn)擊綁定到數(shù)據(jù)源,在Items Source Wizard:
5、選擇數(shù)據(jù)源:
在GridControl選擇table:
選擇簡單綁定模型,查看如下提示消息關(guān)于綁定模型:WPF Data Grid: Bind to Data.
確保CRUD 選項(xiàng)啟動(dòng):
在視圖模型選擇視圖模型選項(xiàng)生成數(shù)據(jù)綁定源碼。在已選擇的View Model中選擇MainViewModel:
6、Items Source Wizard?生成如下代碼:
<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>
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));}}
}
這個(gè)代碼啟動(dòng)CRUD operations?,為每一個(gè)數(shù)據(jù)源字段生成行,然后顯示所有行數(shù)在fixed summary panel.
7、運(yùn)行如下: