我一直在WPF DataGrid和listbox GridView性能显示甚至少量的数据时遇到问题。我虽然这个问题只是WPF一般性能差,但问题似乎只在于文本块控制。WPF文本块性能差
我创建了一个样板,我添加了几个项目。如果我添加简单填充的矩形,调整大小/滚动性能是完美的,但是一旦我使用文本块,性能就会消失。
它看起来像性能问题来自:
child.Measure(constraint);
当文本块被衡量的,它带来的性能嘎然而止。有什么我可以覆盖文本块的测量或某些东西来提高性能? (我会明确设置孩子的大小)
编辑:我现在已经创建了简化的代码来安排项目,因为我想。
这段代码的性能很好,除了......当textblock中文本的宽度超过文本块的实际宽度时。这使我的表现回落到了可能的原因 - 可能是因为它试图再次测量元素?
public class TestPanel : Panel
{
private int _rowHeight = 20;
private int _columnWidth = 50;
public TestPanel()
{
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 20; j++)
{
TextBlock cell = new TextBlock();
cell.ClipToBounds = true;
cell.Width = _columnWidth;
cell.Height = _rowHeight;
cell.Text = i.ToString() + ":" + j.ToString();
this.Children.Add(cell);
}
}
}
protected override Size MeasureOverride(Size constraint)
{
return new Size(_columnWidth*20,_rowHeight*100);
}
protected override Size ArrangeOverride(Size arrangeBounds)
{
UIElementCollection children = InternalChildren;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 20; j++)
{
UIElement child = children[i*20+j];
child.Arrange(new Rect(j * _columnWidth, i * 20, _columnWidth, 20));
}
}
return arrangeBounds;
}
}
public MainWindow()
{
InitializeComponent();
TestPanel myPanel = new TestPanel();
ScrollViewer scroll = new ScrollViewer();
myPanel.Background = Brushes.Aqua;
scroll.Content = myPanel;
this.Content = scroll;
}
看来,即使我覆盖布局面板的度量替代,当文本长于块时,也会出现一个度量。有没有什么好方法可以防止这种情况发生? – ChandlerPelhams