Вопрос

Take a look at the following XAML snippet:

<DataGridTextColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Block.TextAlignment" Value="Center"/>
        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush>
                    <SolidColorBrush.Color>
                        <MultiBinding Converter="{StaticResource VAPBrushConverter}">
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}"/>
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>
                        </MultiBinding>
                    </SolidColorBrush.Color>
                </SolidColorBrush>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTextColumn.CellStyle>

The IValueConverter is being called only when I scroll on the datagrid. Inside the DataGridCell there is a TextBlock, and the only other time the IValueConverter is being called is when the TextBlock.Text property is DependencyProperty.UnsetValue.

Could someone tell me when the IValueConverter is called, and if there is something I can do with my code currently that could resolve this issue ? To clarify - the issue is that the background is only being set by the IValueConverter when I scroll on the DataGrid.

Это было полезно?

Решение

I resolved this issue by setting the binding as the current element in the items source, like this:

<Binding Path="."/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>

It looks like the problem is that the IValueConverter is only called when the get/set accessors are called. I was passing the DataGridCell in the binding which is only get/set when scrolling. The contents of the cell is get/set whenever the value changes, so this means the IValueConverter will update accordingly.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top