Hi,
Just wanted to share a small Grid I created, which can help prevent blurry text etc. as it adjusts the margin of the Grid to ensure a pixel perfect position and size of the grid.
Works great e.g. for inside StackPanels with auto height Labels/TextBlocks.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Controls
{
class PixelGrid : Grid
{
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
// POSITION
Vector position = VisualTreeHelper.GetOffset(this);
double targetX = Math.Round(position.X, MidpointRounding.ToEven);
double targetY = Math.Round(position.Y, MidpointRounding.ToEven);
double marginLeft = targetX - position.X;
double marginTop = targetY - position.Y;
// SIZE
double targetHeight = Math.Round(sizeInfo.NewSize.Height, MidpointRounding.ToEven);
double targetWidth = Math.Round(sizeInfo.NewSize.Width, MidpointRounding.ToEven);
double marginBottom = targetHeight - sizeInfo.NewSize.Height;
double marginRight = targetWidth - sizeInfo.NewSize.Width;
// Adjust margin to ensure pixel width
this.Margin = new Thickness(marginLeft, marginTop, marginRight, marginBottom);
base.OnRenderSizeChanged(sizeInfo);
}
}
}