我是DevExpress网格的新手。当选择一行时,我需要更改网格中一行的颜色。
有人可以发布一些代码来实现上述场景..
提前致谢..
答案 0 :(得分:6)
如果我是你,我会更改GridView.Appearance.FocusedRow.BackColor和GridView.Appearance.SelectedRow.BackColor属性。这将强制GridControl选择此颜色以绘制所选行的背景。
答案 1 :(得分:0)
如果您希望所有网格行的颜色相同(除了选定的行),请忽略此答案以支持DevExpress的帖子。
如果要根据每行后面的ViewModel中的某个变量动态着色网格行,那么这个答案是一个好的开始。
SelectionState
)。不要被代码量拖延。添加锅炉板后,通过将其添加到项目中的任何网格来应用新的配色方案:
RowStyle="{StaticResource CustomRowStyle}"
添加此样式:
<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>
最后,使用此转换器,如果选择或聚焦行,则需要对行进行不同的着色:
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"