北冰洋以北 发表于 2024-6-11 08:59:48

DevExpress WPF中文教程:Grid - 如何向项目添加GridControl并绑定到数据

DevExpress WPF拥有120+个控件和库,将帮助您交付满足以致超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强盛互动功能的XAML基础应用程序,这些应用程序专注于今世客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产物,还是以数据为中心的贸易智能产物,都能通过DevExpress WPF控件来实现。
本教程演示了如何将GridControl添加到项目中,并将控件绑定到数据库:

https://img-blog.csdnimg.cn/img_convert/79999a123a31fa2575ba1c9249602f52.png
获取DevExpress WPF v23.2正式版下载(Q技术交换:532598169)
1. 利用DevExpress Template Gallery(模板库)来创建一个新的Blank MVVM Application(空缺MVVM应用程序),这个项目模版包罗了一个样板View Model类,并设置MainView的数据上下文:

https://img-blog.csdnimg.cn/img_convert/9b4381c8489e31ce4bc6acdbfd4d5f91.png
2. 将项目连接到本地数据库,示比方下:Blank .NET 6 App with the Northwind Database 。
3. 将GridControl工具箱项添加到MainView:

https://img-blog.csdnimg.cn/img_convert/6f26d6c5cc2a14758f2812661c37209d.png
假如您的项目没有DevExpress.Wpf.Grid.Core.v23.2引用,Visual Studio将显示以下消息:

https://img-blog.csdnimg.cn/img_convert/6e4bc8a8ce334f7d2b8646a1b454497a.png
此消息关照您已添加所需的引用,并要求再次添加控件。
提示:假如您从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来启动Items Source Wizard(项目源向导):

https://img-blog.csdnimg.cn/img_convert/eb80da66974ecab8a183f9ba914442d1.png
5. 选择一个数据源:

https://img-blog.csdnimg.cn/img_convert/a2e97df5cbf62c2a45d61cdf959e0d63.png
选择要在GridControl中显示的表:

https://img-blog.csdnimg.cn/img_convert/9716bcf5158f08a5e6833c8ee958c12e.png
选择Simple Binding模子。

https://img-blog.csdnimg.cn/img_convert/da2dddf3503ce3e81f96e1c7c60b28ca.png
确保启用了CRUD选项:

https://img-blog.csdnimg.cn/img_convert/444788c55edc5f6b92df57ec88df1c28.png
选择View Model选项,在View Model中生成数据绑定代码。确认MainViewModel类被选择为View Model:

https://img-blog.csdnimg.cn/img_convert/8cc33dfe32734f106b7ae3914e214867.png
6. Items Source Wizard(项目源向导)生成以下代码:
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;
}
}

public void ValidateRow(RowValidationArgs args) {
var item = (Order)args.Item;
if (args.IsNewItem)
_Context.Orders.Add(item);
_Context.SaveChanges();
}

public void ValidateRowDeletion(ValidateRowDeletionArgs args) {
var item = (Order)args.Items.Single();
_Context.Orders.Remove(item);
_Context.SaveChanges();
}

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操纵,为全部数据源字段生成列,并在固定的汇总面板中显示总行数。
7. 运行项目:

https://img-blog.csdnimg.cn/img_convert/190bba6bfbae812e3117bf27864d5571.png
更多DevExpress线上公开课、中文教程资讯请上中文网获取

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: DevExpress WPF中文教程:Grid - 如何向项目添加GridControl并绑定到数据