如何更改 devexpressgrid 中选定行的颜色

发布于 2024-11-02 09:46:17 字数 90 浏览 0 评论 0原文

我是 DevExpress 网格的新手。当选择一行时,我需要更改网格中行的颜色。

有人可以发布一些代码来实现上述场景..

提前致谢..

I am new to DevExpress grid. I need to change the color of a row in the grid when a row is selected.

Can someone please post some code to achieve the above scenario..

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-11-09 09:46:17

如果我是你,我会更改 GridView.Appearance.FocusedRow.BackColor 和 GridView.Appearance.SelectedRow.BackColor 属性。这将强制 GridControl 选择此颜色来绘制所选行的背景。

If I were you, I would change the GridView.Appearance.FocusedRow.BackColor and GridView.Appearance.SelectedRow.BackColor properties. This will force the GridControl to choose this color to paint the background of a selected row.

失与倦" 2024-11-09 09:46:17

何时忽略此答案

如果您想让所有网格行的颜色相同(除了所选行之外),请忽略此答案,转而使用 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"

When to ignore this answer

Please ignore this answer in favor of the post from DevExpress, if you want to have all grid rows colored the same (apart from the selected row).

If you want to dynamically color the grid rows based on some variable in the ViewModel behind each row, then this answer is a good start.

In a Nutshell

  • Add a new style to the grid.
  • The new style overrides the default color for the row.
  • The new style has a DataTrigger that colors the highlighted row differently when it is selected (by monitoring SelectionState).

It's easy once the boilerplate is added...

Don't be put off by the volume of code. Once you add the boiler plate, the new color scheme is applied by adding this to any grid in your project:

RowStyle="{StaticResource CustomRowStyle}"

The XAML

Add this style:

<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>

Add these colors:

<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>

Then attach this style to your grid using the RowStyle property:

<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>

The C#

Lastly, use this converter, which is needed to color the row differently if it is selected or focused:

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

To hook this converter up, we need to add the standard boiler plate code:

 <converters:TrueIfSelectedOrFocused x:Key="TrueIfSelectedOrFocused" />

and in the header:

 xmlns:converters="clr-namespace:Converters"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文