首页 > 代码库 > dev Gridcontrol根据其cell里面的值显示不同颜色

dev Gridcontrol根据其cell里面的值显示不同颜色

要改变cell值得颜色 需要用到cellTemplate和convert

        <DataTemplate x:Key="PercentageCellColorTemplate">
            <dxe:TextEdit Name="PART_Editor" Mask="p2" MaskType="Numeric" MaskUseAsDisplayFormat="True" FontWeight="Bold" Foreground="{Binding Value, Converter=  {StaticResource       colorConvert}}">
            </dxe:TextEdit>
        </DataTemplate>

Foreground="{Binding Value} 这个value就是这个cell里面的值,在convert中可以根据值得情况来返回不同的颜色值

convert类定义形式如下:

    public class ColorConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string returnValue = "http://www.mamicode.com/Black"; //默认为black
            if (value != null)
            {
                string sValue = http://www.mamicode.com/value.ToString();
                if (sValue != "")
                {
                    double dValue;
                    if (double.TryParse(value.ToString(), out dValue))
                    {
                        if (dValue > 0)
                            returnValue = "http://www.mamicode.com/Red";
                        else if (dValue < 0)
                        {
                            returnValue = "http://www.mamicode.com/Green";
                        }
                        else
                        {
                            returnValue = "http://www.mamicode.com/Black";
                        }
                    }
                }
            }
            return returnValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

            设置改列的cellTemplate属性

                <dxg:GridColumn Header="colorTest" FieldName="test" CellTemplate="{StaticResource PercentageCellColorTemplate}">
                </dxg:GridColumn>

通过上面过程Foreground属性会根据当前cell的值 来得到不同的属性值 从而显示不同的颜色。

dev Gridcontrol根据其cell里面的值显示不同颜色