[WPF]GridView或DataGrid中自定义样式:依据某一列设定其对应行的样式(背景色,字体等)

附效果照一张:

[WPF]GridView或DataGrid中自定义样式:依据某一列设定其对应行的样式(背景色,字体等)

本方法使用StyleSelector来 获得依据自定义逻辑的style。

 class ConditionalStyleSelector : StyleSelector
{
public override Style SelectStyle(object item,DependencyObject container)
{
object conditionValue = this.ConditionConverter.Convert(item, null, null, null);
foreach (ConditionalStyleRule rule in this.Rules)
{
if (Equals(rule.Value, conditionValue))
{
return rule.Style;
}
} return base.SelectStyle(item, container);
} List<ConditionalStyleRule> _Rules;
public List<ConditionalStyleRule> Rules
{
get
{
if (this._Rules == null)
{
this._Rules = new List<ConditionalStyleRule>();
} return this._Rules;
}
} IValueConverter _ConditionConverter;
public IValueConverter ConditionConverter
{
get
{
return this._ConditionConverter;
}
set
{
this._ConditionConverter = value;
}
}
} public class ConditionalStyleRule
{
object _Value;
public object Value
{
get
{
return this._Value;
}
set
{
this._Value = value;
}
} Style _Style;
public Style Style
{
get
{
return this._Style;
}
set
{
this._Style = value;
}
}
}

其中,object conditionValue = this.ConditionConverter.Convert(item, null, null, null);这句话是俺用来进行条件转换的,将输入的条件转换成true或false。(可以转换成各种各样的东西,只要你能在后边拿他来与你定义的样式判断就好)

foreach是俺用来寻找转换后的结果有没有对应样式的

②上面就是主菜,下面说说配菜:

List<ConditionalStyleRule> _Rules;样式的键值对,这个我是在前台xaml里给他赋值的,后边再表。

IValueConverter _ConditionConverter;将输入条件经逻辑判断后返回一个东西来与键值对比较的东西(脱了裤子放屁的设计哈,屎量比较小不怕弄坏裤子的可以放弃他,不用这封装的东西直接与键值对比较)。

public class ConditionalStyleRule{...}这个不用说了吧,封装数据结构的。

③配菜也说完了,嗯,也没大说完,那个IValueConverter还没说:

 class COMStateConverter : IValueConverter
{
#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 == "" ? 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>
<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}"就好了。上面的代码样式我随便塞了个红蓝进去,具体样式自己整吧。哈

上一篇:spring IOC容器实例化Bean的方式与RequestContextListener应用


下一篇:CNN 逐层可视化 Ipython Notebook