附效果照一张:

本方法使用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}"就好了。上面的代码样式我随便塞了个红蓝进去,具体样式自己整吧。哈

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

  1. JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列

    转自   http://blog.csdn.net/tianlincao/article/details/7494467 前言 JQuery easyUi datagrid 中 使用datagrid生 ...

  2. shiro中自定义realm实现md5散列算法加密的模拟

    shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...

  3. WPF学习笔记(7):DataGrid中数字自定义格式显示

    DataGrid中数据显示如下图,数据格式比较杂乱.希望达到以下要求:(1)所有数据保留两位小数:(2)超过1000的数字显示千分位:(3)如果数据为0,不显示. 首先想到用StringFormat进 ...

  4. WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探

    原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探         最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...

  5. wpf 获取datagrid中模板中控件

    //获取name为datagrid中第三列第一行模板的控件 FrameworkElement item = dataGrid.Columns[].GetCellContent(dataGrid.Ite ...

  6. GridView控件中插入自定义删除按钮并弹出确认框

    GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...

  7. 在WPF中自定义你的绘制(五)

    原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五)                                                                   ...

  8. 在WPF中自定义你的绘制(三)

    原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三)                                                                  ...

  9. 在WPF中自定义你的绘制(四)

    原文:在WPF中自定义你的绘制(四)                                   在WPF中自定义你的绘制(四)                                 ...

随机推荐

  1. CSSOM视图模式(CSSOM View Module)相关整理:scrollWidth,scrollLeft,offsetLeft,clientX ,offsetX 定义和区别

    转:http://www.zhangxinxu.com/wordpress/2011/09/cssom%E8%A7%86%E5%9B%BE%E6%A8%A1%E5%BC%8Fcssom-view-mo ...

  2. sql 查询服务器硬盘剩余空间

    DECLARE @tb1 Table( drive varchar(20), [MB 可用空间] varchar(20)) INSERT INTO @tb1 Exec master.dbo.xp_fi ...

  3. 自制c#简易计算器

    这是一个课堂作业,我觉得作为一个简易的计算器不需要态度复杂的东西,可能还有一些bug,有空再慢慢加强. using System;using System.Collections.Generic;us ...

  4. ASP.NET MVC自定义ActionResult实现文件压缩

    有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...

  5. 最简单的RASPBERRY PI wifi配置

    Setting up Wifi with the Command Line  SIMON MONK   This tutorial works best if your router is broad ...

  6. CentOS 6.6编译安装Nginx1.6.2+MySQL5.6.21+PHP5.6.3

    http://www.osyunwei.com/archives/8867.html 一.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables #编辑防火墙配置 ...

  7. C++模拟C#事件委托机制(一)

    原文来自于http://www.cnblogs.com/netssfy/articles/1652671.html 写了一段时间的C#代码后确实发现C#的事件委托非常好用.于是便想是否在C++中也能如 ...

  8. android studio集成融云 SDK 后在部分机型启动对话时崩溃

    最初构建项目是 eclipse, 后来切换到 android studio来做开发. 后来多个用户反馈在android4.4机型上存在启动对话崩溃的问题.但是IOS 版工程从来没有重现. 调试报错信息 ...

  9. PL/SQL Developer记住用户名密码

    在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:PL/SQL Developer ->tools-> ...

  10. AspNetPager控件分页使用方法

    AspNetPager控件官方下载地址:http://www.webdiyer.com/aspnetpager/ 把控件加到项目中(添加自定义控件的方法),并把它拖放到页面上 <asp:Scri ...