如何在devexpressgrid中更改所选行的颜色

|| 我是DevExpress网格的新手。选择行时,我需要更改网格中行的颜色。 有人可以发布一些代码来实现上述方案。 提前致谢..
已邀请:
如果您是我,则将更改GridView.Appearance.FocusedRow.BackColor和GridView.Appearance.SelectedRow.BackColor属性。这将强制GridControl选择该颜色以绘制所选行的背景。
什么时候忽略这个答案 如果要使所有网格行的颜色都相同(除了选定的行),请忽略此答案,以支持DevExpress的发布。 如果要基于每行后面的ViewModel中的某个变量为网格行动态上色,则此答案是一个好的开始。 简而言之 向网格添加新样式。 新样式将覆盖该行的默认颜色。 新样式具有DataTrigger,当选中它时,它会以不同的方式为突出显示的行着色(通过监视
SelectionState
)。 添加样板很容易... 不要被大量的代码拖延。添加样板后,可以通过将新的配色添加到项目中的任何网格来应用新的配色方案:
RowStyle=\"{StaticResource CustomRowStyle}\"
XAML 添加此样式:
<Style x:Key=\"CustomRowStyle\" TargetType=\"{x:Type grid:GridRowContent}\" BasedOn=\"{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}\">
    <Setter Property=\"Foreground\" Value=\"{StaticResource DoneForegroundBrush}\" />
    <Setter Property=\"Background\" Value=\"{StaticResource DoneBackgroundBrush}\" />
    <Style.Triggers>
        <DataTrigger Binding=\"{Binding Path=SelectionState, Converter={StaticResource TrueIfSelectedOrFocused}}\" Value=\"True\">
            <Setter Property=\"Foreground\" Value=\"{StaticResource SelectedForegroundBrush}\" />
            <Setter Property=\"Background\" Value=\"{StaticResource SelectedDoneBackgroundBrush}\" />
        </DataTrigger>
    </Style.Triggers>
</Style>
添加以下颜色:
<SolidColorBrush x:Key=\"DoneForegroundBrush\" Color=\"#FF00D000\"></SolidColorBrush>
<SolidColorBrush x:Key=\"DoneBackgroundBrush\" Color=\"#20263900\"></SolidColorBrush>
<SolidColorBrush x:Key=\"SelectedForegroundBrush\" Color=\"White\"></SolidColorBrush>
<SolidColorBrush x:Key=\"SelectedDoneBackgroundBrush\" Color=\"DarkGreen\" Opacity=\"0.5\"></SolidColorBrush>
然后使用
RowStyle
属性将此样式附加到网格:
<grid:GridControl
          ItemsSource=\"{Binding Items}\"
          SelectedItem=\"{Binding ItemSelected, Mode=TwoWay}\"
          AutoGenerateColumns=\"None\"     
          SelectionMode=\"Row\">
        <grid:GridControl.View>
            <grid:TableView VerticalScrollbarVisibility=\"Auto\"
                AutoWidth=\"True\"
                NavigationStyle=\"Row\"
                DetailHeaderContent=\"Orders\"
                ShowGroupPanel=\"False\"
                ShowColumnHeaders=\"True\"
                FadeSelectionOnLostFocus=\"False\"
                ShowIndicator=\"False\"
                UseLightweightTemplates=\"None\"
                RowStyle=\"{StaticResource CustomRowStyle}\">
            </grid:TableView>
        </grid:GridControl.View>                                    
        <grid:GridControl.Columns>
            <!-- Column definitions here. -->
        </grid:GridControl.Columns>
    </grid:GridControl>
C# 最后,使用此转换器,如果选择或聚焦该行,则需要用不同的颜色对行进行着色:
namespace Converters
{
    public class TrueIfSelectedOrFocused : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                switch ((SelectionState)value)
                {
                    case SelectionState.Selected:
                    case SelectionState.Focused:
                    case SelectionState.FocusedAndSelected:
                        return true;
                    case SelectionState.None:
                        return false;
                    default:
                        return false;
                }
            }
            catch (Exception ex)
            {
                // Log error here.
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
        #endregion
    }
}
为了连接此转换器,我们需要添加标准的样板代码:
 <converters:TrueIfSelectedOrFocused x:Key=\"TrueIfSelectedOrFocused\" />
并在标题中:
 xmlns:converters=\"clr-namespace:Converters\"

要回复问题请先登录注册