Passing a cell's contents into a converter in XAML

526 views Asked by At

I'm trying to create a style that aligns a cell based on the type of its contents. If it's text, it's aligned to the left, if it's a number, it's aligned to the right. I've tried to create a converter that returns an alignment based on the value given to it. Here is the code:

<Style x:Key="Align" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="{Binding Converter={StaticResource SmartAlignmentConverter}}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The problem is that the converter sends in the row object, rather than the cell. As in, the entire object that contains the row's variables is sent in, so there's no usable info about which specific cell is being aligned. I need some way to send in the cell's value specifically.

UPDATE:

I've managed to send in the cell itself, which is a step in the right direction.

Binding="{Binding RelativeSource={RelativeSource self}, Converter={StaticResource SmartAlignmentConverter}}" 

But the problem has become that there's no information inside of the cell. It's content is null when it's sent to the converter. I don't suppose there's a way to send the thing to the converter after it's been filled with information?

1

There are 1 answers

2
brunnerh On

RelativeSource={RelativeSource AncestorType=DataGridCell}?