我一直在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