A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
Hello @Миров Сергей ,
Thanks for your question.
When you hover over DataGrid rows, WPF is creating brand new Brush objects (like SolidColorBrush) for every mouse movement, instead of reusing the same brush. It's like creating a new paintbrush for each stroke instead of using one paintbrush over and over.
I recommend some following steps:
- You can use
StaticResourcefor brushes instead ofDynamicResourcesinceStaticResourcecreates the brush once at compile time and reuses it.DynamicResourcecan create new instances at runtime. - I suggest defining brushes in resources section, not inline. Instead of pasting the brush definition directly in each style trigger (which means each row gets its own copy), define it once in your resources and point to it. All rows then share that same single brush.
- If you're creating brushes in code, call
.Freeze()on them. This basically locks the brush so WPF knows it won't change, which lets it optimize and reuse it. Saves a lot of memory. - Please avoid per-item templates with inline brushes. Each row template instance would create its own brushes. Share brushes via resources instead.
For more information, you can refer to theses documents:
- Optimizing WPF Application Performance.
- Optimizing Performance: Application Resources.
- Freezable Objects Overview
Furthermore, you don't need to override the DataGridRow style or mess around with MouseEnter/MouseLeave events. That would be overkill and more work than it's worth. And this is not caused by SnapsToDevicePixels or layout rounding. It's a lightweight visual property - it doesn't create or allocate brushes at all. Setting it to true or false has basically no effect on your memory issue.
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.