翻譯|使用教程|編輯:龔雪|2021-02-23 09:39:22.757|閱讀 408 次
概述:DevExpress WPF擁有120+個(gè)控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序,本文將為大家介紹如何綁定到列集合。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
下載DevExpress v20.2完整版 DevExpress v20.2漢化資源獲取
DevExpress WPF 擁有120+個(gè)控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。
使用模型視圖ViewModel(MVVM)架構(gòu)模式設(shè)計(jì)WPF應(yīng)用程序時(shí),可能需要描述模型或ViewModel中的列。 網(wǎng)格可以綁定到包含列設(shè)置的對(duì)象集合,該對(duì)象設(shè)置在Model或ViewModel中進(jìn)行了描述,從而最大限度地減少了“隱藏代碼”的需求。
假設(shè)一個(gè)雇員視圖模型,它包括以下類:
C#
using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; namespace Model { public class ViewModel { public List<string> Cities { get; private set; } // Returns a list of employees so that they can be bound to the grid control. public List<Employee> Source { get; private set; } // The collection of grid columns. public ObservableCollection<Column> Columns { get; private set; } public ViewModel() { Source = EmployeeData.DataSource; List<string> _cities = new List<string>(); foreach (Employee employee in Source) { if (!_cities.Contains(employee.City)) _cities.Add(employee.City); } Cities = _cities; Columns = new ObservableCollection<Column>() { new Column() { FieldName = "FirstName", Settings = SettingsType.Default }, new Column() { FieldName = "LastName", Settings = SettingsType.Default }, new Column() { FieldName = "JobTitle", Settings = SettingsType.Default }, new Column() { FieldName = "BirthDate", Settings = SettingsType.Default }, new ComboColumn() { FieldName = "City", Settings = SettingsType.Combo, Source = Cities } }; } } // The data item. public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string JobTitle { get; set; } public string City { get; set; } public DateTime BirthDate { get; set; } } public class EmployeeData : List<Employee> { public static List<Employee> DataSource { get { List<Employee> list = new List<Employee>(); list.Add(new Employee() { FirstName = "Nathan", LastName = "White", City = "NY", JobTitle = "Sales Manager", BirthDate = new DateTime(1970, 1, 10) }); return list; } } } public class Column { // Specifies the name of a data source field to which the column is bound. public string FieldName { get; set; } // Specifies the type of an in-place editor used to edit column values. public SettingsType Settings { get; set; } } // Corresponds to a column with the combo box in-place editor. public class ComboColumn : Column { // The source of combo box items. public IList Source { get; set; } } public enum SettingsType { Default, Combo } }
注意:如果將Columns集合分配給網(wǎng)格控件后可能會(huì)更改,則它應(yīng)實(shí)現(xiàn)INotifyCollectionChanged,以便網(wǎng)格中可自動(dòng)反映View Model內(nèi)所做的更改。
網(wǎng)格控件基于列模板生成列,創(chuàng)建多個(gè)模板,每種列類型一個(gè)模板。使用單個(gè)模板,您可以在無限數(shù)量的網(wǎng)格控件中創(chuàng)建無限數(shù)量的列。 在此示例中,有兩個(gè)列模板:DefaultColumnTemplate和ComboColumnTemplate。
為避免綁定到列屬性時(shí)的性能問題,請(qǐng)使用dxci:DependencyObjectExtensions.DataContext附加屬性,請(qǐng)參見下面的示例。
XAML
<!----> xmlns:dxci="http://schemas.devexpress.com/winfx/2008/xaml/core/internal" <!----> <DataTemplate x:Key="DefaultColumnTemplate"> <ContentControl> <dxg:GridColumn FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}" Header="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Header, RelativeSource={RelativeSource Self}}" Width="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Width, RelativeSource={RelativeSource Self}}" /> </ContentControl> </DataTemplate>
要根據(jù)列的類型選擇所需的模板,請(qǐng)使用模板選擇器。 在此示例中,模板選擇器由ColumnTemplateSelector類表示。
XAML
<Window x:Class="WpfApplication10.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxci="http://schemas.devexpress.com/winfx/2008/xaml/core/internal" xmlns:model="clr-namespace:Model" xmlns:view="clr-namespace:View"> <Window.DataContext> <model:ViewModel/> </Window.DataContext> <Window.Resources> <view:ColumnTemplateSelector x:Key="ColumnTemplateSelector"/> <DataTemplate x:Key="DefaultColumnTemplate"> <ContentControl> <dxg:GridColumn FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"/> </ContentControl> </DataTemplate> <DataTemplate x:Key="ComboColumnTemplate"> <ContentControl> <dxg:GridColumn FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"> <dxg:GridColumn.EditSettings> <dxe:ComboBoxEditSettings ItemsSource="{Binding Source}"/> </dxg:GridColumn.EditSettings> </dxg:GridColumn> </ContentControl> </DataTemplate> </Window.Resources> <Grid> </Grid> </Window>
C#
using System.Windows; using System.Windows.Controls; using Model; namespace View { public class ColumnTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { Column column = (Column)item; return (DataTemplate)((Control)container).FindResource(column.Settings + "ColumnTemplate"); } } }
注意:如果可以使用單個(gè)模板描述所有網(wǎng)格列,則無需創(chuàng)建列模板選擇器,而是將此模板分配給網(wǎng)格的屬性。
注意:您可以創(chuàng)建一種樣式來指定使用不同模板生成的所有列共有的設(shè)置,您可以在樣式內(nèi)指定對(duì)ViewModel屬性的綁定(請(qǐng)參見下面的FieldName):
XAML
<Window.Resources> <Style x:Key="ColumnStyle" TargetType="dxg:GridColumn"> <Setter Property="FilterPopupMode" Value="CheckedList"/> <Setter Property="FieldName" Value="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"/> </Style> </Window.Resources>
該樣式應(yīng)分配給屬性。
最后,指定網(wǎng)格的,和。屬性指定網(wǎng)格的數(shù)據(jù)源,屬性指定網(wǎng)格從中生成列的源,屬性指定列模板選擇器,該選擇器根據(jù)其類型為每個(gè)列返回一個(gè)模板。
XAML
<Grid> <dxg:GridControl Name="grid" ItemsSource="{Binding Source}" ColumnsSource="{Binding Columns}" ColumnGeneratorTemplateSelector="{StaticResource ColumnTemplateSelector}"> <dxg:GridControl.View> <dxg:TableView Name="tableView1" AutoWidth="True" NavigationStyle="Cell" /> </dxg:GridControl.View> </dxg:GridControl> </Grid>
下圖顯示了結(jié)果。
DevExpress技術(shù)交流群3:700924826 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)