Question

I have two DataGrids and i have synchronous scrolling in place (scrolling on one grid causes other to scroll). One of the grids has a DataGridTemplateColumn with DataTemplate set to an Image:

<DataGridTemplateColumn Width="16">
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="CommentIcon_Clicked" />
        </Style>
    </DataGridTemplateColumn.CellStyle>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Blank, Mode=TwoWay}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

After data is bound to the grids, scrolling becomes very sluggish and slow. Therefore i suspect its the Source property binding operation that causes the sluggishness everytime scrolling occurs because if i set the binding to a StaticResource, then the scrolling becomes smooth. Is there a way to fix this?

Update The problem is because of the binding errors to the Image source. The value of the property that binds to the Image source is an empty string:

var d2 = (from l in t.Item2
          select new ProgramLine { Blank = String.Emtpty, Line = l}).ToList();
dataGrid2.ItemsSource = d2;

This was causing the default converter to fail and hence cause the scrolling to become sluggish because of the property getter getting called everytime the scrolling occurred.

Lesson learnt Like Steven says, make sure there are no binding errors

Tip Use the immediate window when running your WPF application in debug mode

Was it helpful?

Solution

Look for binding errors in the VS Output window. Also you can use this attached property directly on your binding to image source: diagnostics:PresentationTraceSources.TraceLevel=High Namespace diagnostics is here: xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase" After that, you can find even more info about your binding again, in your Output window. Unfortunately the binding errors are capable of HUGE performance hit. So try to completely eliminate them always when you are working on any WPF application.

Btw you dont have to make your binding TwoWay, because Image control just cant change the property value back. Its OneWay control that just accepts the given value.

EDIT: There are some WPF DataGrid control performance tips you can make use of link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top