WPF performance when updating items

I am currently developing a GUI for the Ising model (German Wikipedia, because only the photograph on the right actually matters), which should consist of approximately 200x200 spin elements. I implemented this as follows:

<UniformGrid Name="grid" .... />

and added a rectangle for each spin in the code, behind which I update if the spin value changes. It was somehow very slow, and I changed it, so it uses the binding

 <ItemsControl Name="IsingLattice" ItemsSource="{Binding Spins}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Name="grid" ...
            ...
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Rectangle Fill={Binding Color} ...

but again it is very slow. I tried to debug and improve it for 3 days, but so far I have not achieved anything.

Now the question is: is my approach appropriate? What should be used instead, if so? If not, how can I improve performance?

, .

: , . , , , .

+4
4

ItemsControl . , . . .

- , , . ItemsControl Image ItemsSource WritableBitmap .

, :

  • , ItemsSource,
  • UniformGrid . . Canvas .
  • ItemsControl 40 000 DataTemplate.
  • . , 40 000 . .

, 500 . , WritableBitmap , .

+3

( 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
    }
}

, . .

+6

, , WPF Windows Forms - , . .

(, 40 000 ), . , , .

, , . , , .

0

BitmapCache ?

I understand that this can significantly improve the rendering speed when drawing complex controls or while having a large number of controls on the screen. You want to enable cache at the grid level, and not at each individual counter.

0
source

All Articles