devExpress

DevExpress WPF DataGrid 如何根据某列内容来改变整行颜色

时间:2014-5-24 11:32:37  作者:WPF之家  来源:http://www.wpf123.com/  查看:4048  评论:0
内容摘要:<Window x:Class="ConditionalRowFormatting.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x...
<Window x:Class="ConditionalRowFormatting.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
    xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"     xmlns:local="clr-namespace:ConditionalRowFormatting" 
    Title="Window1" 
    Height="430" 
    Width="610">     <Window.Resources>         <ResourceDictionary>             <local:SimpleDataList x:Key="simpleSource" InitCount="25" />             <Style x:Key="BackgroundStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}">          //重点:这里Binding的是 Row.Int 也就是行中哪一列的值.                 <Setter Property="Background" Value="{Binding Path=Row.Int, Converter={local:ColorValueConverter MaxValue=25}}" />             </Style>         </ResourceDictionary>     </Window.Resources>     <Grid>         <dxg:GridControl ItemsSource="{StaticResource simpleSource}" AutoGenerateColumns="AddNew">             <dxg:GridControl.View>                 <dxg:TableView AutoWidth="True" RowStyle="{StaticResource BackgroundStyle}" />             </dxg:GridControl.View>         </dxg:GridControl>     </Grid> </Window>

值转换器:
public class ColorValueConverter : MarkupExtension, IValueConverter {
        public int MaxValue { get; set; }
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            int count = MaxValue - (int)value;
            return new SolidColorBrush(GetGradientColor(count));
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
        public override object ProvideValue(IServiceProvider serviceProvider) {
            return this;
        }
        private Color GetGradientColor(int count) {
            Color color = Color.FromRgb(0xff, 0xad, 0x5d);
            byte r = (byte)(color.R + (0xff - color.R) * count / MaxValue);
            byte g = (byte)(color.G + (0xff - color.G) * count / MaxValue);
            byte b = (byte)(color.B + (0xff - color.B) * count / MaxValue);
            return Color.FromArgb(255, r, g, b);
        }
    }

标签:如何 根据 内容 改变 整行 DevExpress  WPF  DataGrid 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

上一篇:没有了
下一篇:Devexpress gridcontrol设置列样式
相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号