附效果照一张:
本方法使用StyleSelector来 获得依据自定义逻辑的style。
①
1 class ConditionalStyleSelector : StyleSelectorView Code
2 {
3 public override Style SelectStyle(object item,DependencyObject container)
4 {
5 object conditionValue = this.ConditionConverter.Convert(item, null, null, null);
6 foreach (ConditionalStyleRule rule in this.Rules)
7 {
8 if (Equals(rule.Value, conditionValue))
9 {
10 return rule.Style;
11 }
12 }
13
14 return base.SelectStyle(item, container);
15 }
16
17 List<ConditionalStyleRule> _Rules;
18 public List<ConditionalStyleRule> Rules
19 {
20 get
21 {
22 if (this._Rules == null)
23 {
24 this._Rules = new List<ConditionalStyleRule>();
25 }
26
27 return this._Rules;
28 }
29 }
30
31 IValueConverter _ConditionConverter;
32 public IValueConverter ConditionConverter
33 {
34 get
35 {
36 return this._ConditionConverter;
37 }
38 set
39 {
40 this._ConditionConverter = value;
41 }
42 }
43 }
44
45 public class ConditionalStyleRule
46 {
47 object _Value;
48 public object Value
49 {
50 get
51 {
52 return this._Value;
53 }
54 set
55 {
56 this._Value = value;
57 }
58 }
59
60 Style _Style;
61 public Style Style
62 {
63 get
64 {
65 return this._Style;
66 }
67 set
68 {
69 this._Style = value;
70 }
71 }
72 }
其中,object conditionValue = this.ConditionConverter.Convert(item, null, null, null);这句话是俺用来进行条件转换的,将输入的条件转换成true或false。(可以转换成各种各样的东西,只要你能在后边拿他来与你定义的样式判断就好)
foreach是俺用来寻找转换后的结果有没有对应样式的
②上面就是主菜,下面说说配菜:
List<ConditionalStyleRule> _Rules;样式的键值对,这个我是在前台xaml里给他赋值的,后边再表。
IValueConverter _ConditionConverter;将输入条件经逻辑判断后返回一个东西来与键值对比较的东西(脱了裤子放屁的设计哈,屎量比较小不怕弄坏裤子的可以放弃他,不用这封装的东西直接与键值对比较)。
public class ConditionalStyleRule{...}这个不用说了吧,封装数据结构的。
③配菜也说完了,嗯,也没大说完,那个IValueConverter还没说:
class COMStateConverter : IValueConverterView Code
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
COM tempCOM = value as COM;
if (tempCOM != null)
{
return tempCOM.StateCode == "0" ? true : false;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
就是一IValueConverter接口,实现了源数据类型以及目标数据类型之间的转换。具体咋回事度娘去吧。
④最后说一下前台xaml的使用
<UserControl.Resources>View Code
<Style x:Key="HighUnitPriceStyle"
TargetType="GridViewRow">
<Setter Property="Background" Value="red" />
<Setter Property="Foreground" Value="blue" />
</Style>
<Style x:Key="LowUnitPriceStyle" TargetType="GridViewRow"/>
<cc:COMStateConverter x:Key="Convert" />
<cc:ConditionalStyleSelector x:Key="selector" ConditionConverter="{StaticResource Convert}">
<cc:ConditionalStyleSelector.Rules>
<cc:ConditionalStyleRule Style="{StaticResource HighUnitPriceStyle}">
<cc:ConditionalStyleRule.Value>
<sys:Boolean>True</sys:Boolean>
</cc:ConditionalStyleRule.Value>
</cc:ConditionalStyleRule>
<cc:ConditionalStyleRule Style="{StaticResource LowUnitPriceStyle}">
<cc:ConditionalStyleRule.Value>
<sys:Boolean>False</sys:Boolean>
</cc:ConditionalStyleRule.Value>
</cc:ConditionalStyleRule>
</cc:ConditionalStyleSelector.Rules>
</cc:ConditionalStyleSelector>
</UserControl.Resources>
然后在DataGrid或GridView中塞进RowStyleSelector="{StaticResource selector}"就好了。上面的代码样式我随便塞了个红蓝进去,具体样式自己整吧。哈