C
C#2d ago
Dyda

✅ Change dataGridRow:selected row

Hi, I’m encountering an issue with changing the selected row color in a DataGrid. I’m using something like this: <Application.Styles> <FluentTheme> <FluentTheme.Palettes> <ColorPaletteResources x:Key="Light" Accent="Green" RegionColor="White" ErrorText="Red" /> <ColorPaletteResources x:Key="Dark" Accent="Yellow" RegionColor="#2D2D2D" ErrorText="Red" /> </FluentTheme.Palettes> </FluentTheme> <StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/> <Style Selector="DataGridRow:selected"> <Setter Property="Foreground" Value="Black" /> <Setter Property="Background" Value="LightBlue" /> </Style> </Application.Styles> After selection, the foreground color updates, but the background color remains yellow (inherited from FluentTheme) and no error are shown.
5 Replies
FusedQyou
FusedQyou2d ago
This is Avalonia, right? One thing with styling is that you might have to point to an inner control inside of the control instead of being able to pass the style property directly. Your foreground works, but the background is not able to be modified directly because the properly is not delegated to the actual control that sets the background. One thig I like to do is find the control as it is deifned by the theme. In your case the fluent theme.
FusedQyou
FusedQyou2d ago
GitHub
Avalonia/src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml at 11745...
Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology - AvaloniaUI/Avalonia
FusedQyou
FusedQyou2d ago
GitHub
Avalonia/src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml at 11745...
Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology - AvaloniaUI/Avalonia
FusedQyou
FusedQyou2d ago
So there's likely two things that have to be changed - The selector in your case has to have /template/ Rectangle#BackgroundRectangle appended to point to the correct control, as specified in my previous message. You have to set a rectangle in the grid row that explicitly specifies the background color. - Instead of setting the background, it appears you have to set the fill. The reason for this change is because you are not setting the color of a rectangle, and not a border which would have a background instead.
Dyda
DydaOP2d ago
Ok, it’s working. Thank you

Did you find this page helpful?