views:

53

answers:

2

I created a converter, and assigned it to a style.

than i assigned that style, to the columns i want affected.

as rows are added, and while stepping through debugger, i noticed that the converter convert method gets called 1 time per column (each time it is used).

is there a way to optimize it better, so that it gets called only once and all columns using it get the same value?

 <Style x:Key="ConditionalColorStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource CellStyle}">
        <Setter Property="Foreground">
            <Setter.Value>
                <Binding>
                    <Binding.Converter>
                        <local:ConditionalColorConverter />
                    </Binding.Converter>
                </Binding>
            </Setter.Value>
        </Setter>
    </Style>
A: 

Perhaps you could cache the result of the converter in a member variable.

        if (this._result == null)
            this._result = this.LookupStyle();
        return this._result;
Chris McKenzie
thanks, caching is definitely a good idea.. just adds complexity and maintenance..
Sonic Soul
A: 

i realized that the properties i am setting to each of the cells, can also be set to the row. so i assigned the converter to the row.

The cells that do have these styles set, will NOT be affected by the converter, as cell style takes precedence over row style.

so there is some trade off in cell level flexibility vs Convert executed for each column.

Sonic Soul