翻譯|使用教程|編輯:龔雪|2021-02-23 09:39:22.757|閱讀 408 次
概述:DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序,本文將為大家介紹如何綁定到列集合。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
下載DevExpress v20.2完整版 DevExpress v20.2漢化資源獲取
DevExpress WPF 擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有著強大互動功能的XAML基礎應用程序,這些應用程序專注于當代客戶的需求和構建未來新一代支持觸摸的解決方案。
使用模型視圖ViewModel(MVVM)架構模式設計WPF應用程序時,可能需要描述模型或ViewModel中的列。 網格可以綁定到包含列設置的對象集合,該對象設置在Model或ViewModel中進行了描述,從而最大限度地減少了“隱藏代碼”的需求。
假設一個雇員視圖模型,它包括以下類:
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集合分配給網格控件后可能會更改,則它應實現INotifyCollectionChanged,以便網格中可自動反映View Model內所做的更改。
網格控件基于列模板生成列,創建多個模板,每種列類型一個模板。使用單個模板,您可以在無限數量的網格控件中創建無限數量的列。 在此示例中,有兩個列模板:DefaultColumnTemplate和ComboColumnTemplate。
為避免綁定到列屬性時的性能問題,請使用dxci:DependencyObjectExtensions.DataContext附加屬性,請參見下面的示例。
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>
要根據列的類型選擇所需的模板,請使用模板選擇器。 在此示例中,模板選擇器由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"); } } }
注意:如果可以使用單個模板描述所有網格列,則無需創建列模板選擇器,而是將此模板分配給網格的屬性。
注意:您可以創建一種樣式來指定使用不同模板生成的所有列共有的設置,您可以在樣式內指定對ViewModel屬性的綁定(請參見下面的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>
該樣式應分配給屬性。
最后,指定網格的,和。屬性指定網格的數據源,屬性指定網格從中生成列的源,屬性指定列模板選擇器,該選擇器根據其類型為每個列返回一個模板。
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>
下圖顯示了結果。
DevExpress技術交流群3:700924826 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網