關於DevExpress WPF DataGrid 如何根據某列內容來改變整行顏色


<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);
        }
    }
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM