我正在考虑将我的一个应用程序转换为 WPF,我主要关心的问题之一是DataGrid
控件的性能(我用户的计算机是过时的 XP 机器,没有专用图形)。我尝试了几千行,滚动性能很糟糕。
我想知道是否有办法将数据网格单元格的内容设置为派生自的自定义类FrameworkElement
?
这是一个示例类,当放入虚拟化堆栈面板时,它的性能似乎要好得多:
public class LiteTextBlock : FrameworkElement
{
private Size _mySize;
private string _myText;
private double totalWidth;
private GlyphTypeface _typeface;
private int _fontSize;
private GlyphRun _run;
public LiteTextBlock(Size mySize, string myText, GlyphTypeface typeface, int fontSize)
{
_mySize = mySize;
this.Width = _mySize.Width;
this.Height = _mySize.Height;
_myText = myText + " additional information";
_typeface = typeface;
_fontSize = fontSize;
}
protected override void OnRender(DrawingContext drawingContext)
{
if (_run == null)
{
totalWidth = 0;
ushort[] glyphIndexes = new ushort[_myText.Length];
double[] advanceWidths = new double[_myText.Length];
for (int i = 0; i < _myText.Length; i++)
{
ushort glyphIndex = _typeface.CharacterToGlyphMap[_myText[i]];
double width = _typeface.AdvanceWidths[glyphIndex] * _fontSize;
if (totalWidth + width >= _mySize.Width - 10)
{
Array.Resize(ref glyphIndexes, i);
Array.Resize(ref advanceWidths, i);
break;
}
glyphIndexes[i] = glyphIndex;
advanceWidths[i] = width;
totalWidth += width;
}
Point origin = new Point(5, 0);
_run = new GlyphRun(_typeface, 0, false, _fontSize, glyphIndexes
, origin, advanceWidths, null, null, null, null, null, null);
}
drawingContext.DrawGlyphRun(Brushes.Black, _run);
}
}
谁能告诉我这是否可能?
免责声明:我已经尝试过使用轻量级控件,例如ListView
,但性能仍然很差。