关于c#:WPF界面应用开发技巧绑定到条件格式规则的集合

11次阅读

共计 6929 个字符,预计需要花费 18 分钟才能阅读完成。

点击获取工具 >>
DevExpress WPF 领有 120+ 个控件和库,将帮忙您交付满足甚至超出企业需要的高性能业务应用程序。通过 DevExpress WPF 能创立有着弱小互动性能的 XAML 根底应用程序,这些应用程序专一于当代客户的需要和构建将来新一代反对触摸的解决方案。

应用模型视图 ViewModel(MVVM)架构模型设计 WPF 应用程序时,可能须要在模型或视图模型中形容条件格局设置规定。网格能够绑定到蕴含条件格式化规定的对象汇合,这些条件在 Model 或 ViewModel 中进行了形容,从而最大限度地缩小了“暗藏代码”的需要。

View 模型实现

本文中应用的示例蕴含一个视图模型代码,其中包含以下类。

  • Order – 蕴含订单信息(例如数量,折扣等)的数据对象。
  • ViewModel – 订单视图模型,_ViewModel_公开 Orders 属性(在网格内显示的订单列表)和 Rules 属性(格局规定列表)。
  • OrderData – 提供要在网格控件中显示的订单信息。
  • FormattingRule – 形容条件格局设置规定,此类提供的属性与所有类型的条件格局设置规定通用的设置绝对应。
  • FormattingType – 枚举利用格局的可能类型。

C#

`using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace GridDemo {
// …
public class ViewModel {
public ViewModel() {
Orders = GetOrders();
Rules = GetFormattingRules();
}

private static List<FormattingRule> GetFormattingRules() {
var rules = new List<FormattingRule>();
rules.Add(new FormattingRule() {
Expression = “([UnitPrice] [Quantity] (1 – [Discount]) – [Freight]) < 0″,
ApplyToRow = true,
Type = FormattingType.Background
});
rules.Add(new FormattingRule() {
FieldName = “Discount”,
Expression = “[Discount] > 0”,
ApplyToRow = false,
Type = FormattingType.Font
});
rules.Add(new FormattingRule() {
FieldName = “Discount”,
Expression = “[Discount] > 0”,
Type = FormattingType.Icon
});
return rules;
}

private static List<Order> GetOrders() {
List<Order> list = new List<Order>();
list.Add(new Order() {City = “Aachen”, UnitPrice = 10, Quantity = 20, Discount = 0, Freight = 30.54});
// …
return list;
}

// A list of orders displayed within the grid control.
public List<Order> Orders {get; private set;}

// A list of conditional formatting rules.
public List<FormattingRule> Rules {get; private set;}
}

// Corresponds to an order items displayed within grid.
public class Order {
public string City {get; set;}
public double Discount {get; set;}
public double Freight {get; set;}
public double Quantity {get; set;}
public double UnitPrice {get; set;}
}

public enum FormattingType {Icon, Font, Background}

public class FormattingRule {
public virtual bool ApplyToRow {get; set;}
public virtual string Expression {get; set;}
public virtual string FieldName {get; set;}
public virtual FormattingType Type {get; set;}
}
}`

留神 :如果在将格局条件汇合调配给网格控件之后可能对其进行了更改,则它应实现 INotifyCollectionChanged,以便网格中能够主动反映在视图模型内进行的更改。

格式化条件模板和选择器

GridControl 基于条件格局模板生成条件格局。创立多个模板,每种条件格局类型一个模板。应用单个模板,您能够在有限数量的网格控件中创立有限数量的条件格局。在此示例中,存在三种条件格局模板:FontFormat、BackgroundFormat 和 IconFormat。

为防止绑定到列属性时的性能问题,请应用 dxci:DependencyObjectExtensions.DataContext 附加属性。请参见上面的示例。

XAML

`<!—->
xmlns:dxci=”http://schemas.devexpress.com/winfx/2008/xaml/core/internal”
<!—->
<DataTemplate x:Key=”FontFormat”>
<ContentControl>
<dxg:FormatCondition
ApplyToRow=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).ApplyToRow, RelativeSource={RelativeSource Self}}”
Expression=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Expression, RelativeSource={RelativeSource Self}}”
FieldName=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}”>
<dxg:FormatCondition.Format>
<dx:Format FontWeight=”Bold” />
</dxg:FormatCondition.Format>
</dxg:FormatCondition>
</ContentControl>
</DataTemplate>`

要依据列的类型抉择所需的模板,请应用模板选择器。在此示例中,模板选择器由 FormatConditionSelector 类示意。

XAML

`<Window
x:Class=”GridDemo.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″
xmlns:local=”clr-namespace:GridDemo”
xmlns:dxg=”http://schemas.devexpress.com/winfx/2008/xaml/grid”
xmlns:dx=”http://schemas.devexpress.com/winfx/2008/xaml/core”
xmlns:dxci=”http://schemas.devexpress.com/winfx/2008/xaml/core/internal”
xmlns:dxt=”http://schemas.devexpress.com/winfx/2008/xaml/core/themekeys”
Height=”350″
Width=”525″
mc:Ignorable=”d”
Title=”MainWindow”>

<Window.DataContext>
<local:ViewModel />
</Window.DataContext>

<Grid>
<Grid.Resources>
<DataTemplate x:Key=”FontFormat”>
<ContentControl>
<dxg:FormatCondition
ApplyToRow=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).ApplyToRow, RelativeSource={RelativeSource Self}}”
Expression=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Expression, RelativeSource={RelativeSource Self}}”
FieldName=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}”>
<dxg:FormatCondition.Format>
<dx:Format FontWeight=”Bold” />
</dxg:FormatCondition.Format>
</dxg:FormatCondition>
</ContentControl>
</DataTemplate>
<DataTemplate x:Key=”BackgroundFormat”>
<ContentControl>
<dxg:FormatCondition
ApplyToRow=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).ApplyToRow, RelativeSource={RelativeSource Self}}”
Expression=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Expression, RelativeSource={RelativeSource Self}}”
FieldName=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}”>
<dxg:FormatCondition.Format>
<dx:Format Background=”LightPink” />
</dxg:FormatCondition.Format>
</dxg:FormatCondition>
</ContentControl>
</DataTemplate>
<DataTemplate x:Key=”IconFormat”>
<ContentControl>
<dxg:FormatCondition
ApplyToRow=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).ApplyToRow, RelativeSource={RelativeSource Self}}”
Expression=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Expression, RelativeSource={RelativeSource Self}}”
FieldName=”{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}”>
<dxg:FormatCondition.Format>
<dx:Format Icon=”{dx:IconSet Name=Stars3_1}” />
</dxg:FormatCondition.Format>
</dxg:FormatCondition>
</ContentControl>
</DataTemplate>
<local:FormatConditionSelector
x:Key=”selector”
BackgroundTemplate=”{StaticResource BackgroundFormat}”
FontTemplate=”{StaticResource FontFormat}”
IconTemplate=”{StaticResource IconFormat}” />
</Grid.Resources>

</Grid>

</Window>`

C#

`public class FormatConditionSelector : DataTemplateSelector {
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
if(!(item is FormattingRule)) return null;
var vm = item as FormattingRule;
switch(vm.Type) {
case FormattingType.Icon:
return IconTemplate;
case FormattingType.Font:
return FontTemplate;
case FormattingType.Background:
return BackgroundTemplate;
default: return null;
}
}

public DataTemplate BackgroundTemplate {get; set;}
public DataTemplate FontTemplate {get; set;}
public DataTemplate IconTemplate {get; set;}
}`

留神 :如果能够应用单个模板形容所有条件格局,则无需创立模板选择器。而是,将此模板调配给网格视图的 TableView.FormatConditionGeneratorTemplate(TreeListView 的 TreeListView.FormatConditionGeneratorTemplate)属性。

自定义 GridControl

最初,指定网格视图的 FormatConditionGeneratorTemplateSelector 和 FormatConditionsSource 属性。TableView.FormatConditionsSource(TreeListView.FormatConditionsSource)属性指定网格生成条件格局的起源,TableView.FormatConditionGeneratorTemplateSelector (TreeListView.FormatConditionGeneratorTemplateSelector) 属性指定模板选择器,该选择器依据其类型为每种条件格局返回一个模板。

XAML

`<dxg:GridControl
AutoGenerateColumns=”AddNew”
EnableSmartColumnsGeneration=”True”
ItemsSource=”{Binding Orders}”>
<dxg:GridControl.View>
<dxg:TableView
FormatConditionGeneratorTemplateSelector=”{StaticResource selector}”
FormatConditionsSource=”{Binding Rules}” />
</dxg:GridControl.View>
</dxg:GridControl>`

下图显示了后果。

正文完
 0