更新元素时​​的WPF性能

oer*_*pli 4 .net c# wpf performance xaml

我目前正在开发一个Ising模型的GUI (德国维基百科,因为只有右边的图片才真正重要),它应该包含大约200x200的自旋元素.我通过以下方式实现了这个:

<UniformGrid Name="grid" .... />
Run Code Online (Sandbox Code Playgroud)

并为代码中的每个旋转添加一个矩形,如果旋转的值发生变化,我将更新该代码.这种方式非常慢,我改变它,所以它使用Binding

 <ItemsControl Name="IsingLattice" ItemsSource="{Binding Spins}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Name="grid" ...
            ...
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Rectangle Fill={Binding Color} ...
Run Code Online (Sandbox Code Playgroud)

但这又是 - 非常慢.我试图调试并改进它3天,但到目前为止没有成功.

现在的问题是:我的方法是错的吗?如果是这样我应该使用什么?如果不是 - 我怎样才能提高性能?

如果它是相关的,我将更新这篇文章,其中包含我的模型实现的一些细节.

编辑:应该可以通过与元素交互来更改单个旋转.这可以通过实际图形顶部的透明层完成,但也许不是那么难.

Ste*_*nds 6

您可以编写一个自定义元素(派生自FrameworkElement内部),在内部存储旋转数据,然后通过覆盖OnRender方法在一次传递中呈现数据:

public sealed class IsingModel : FrameworkElement
{
    readonly bool[] _spinData = new bool[200 * 200];

    protected override void OnRender(DrawingContext dc)
    {
        // use methods of DrawingContext to draw appropriate
        // filled squares based on stored spin data
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);

        // work out which "cell" was clicked on and change
        // appropriate spin state value in Boolean array

        InvalidateVisual(); // force OnRender() call
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法应该比拥有数千个单独的元素更快.我不知道要快多少.