views:

19

answers:

1

Hi there,

This is hopefully going to be a really simple answer, I'm just not seeing the proverbial wood for the trees I think.

I've got a DataGridCell style in which I want to bind the content of the cell to the source property of an image, here's the XAML I'm using at the moment:

<Style x:Key="DataGridImageCellStyle" TargetType="{x:Type toolkit:DataGridCell}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type toolkit:DataGridCell}">
                <Border Background="Transparent" 
              BorderBrush="{TemplateBinding BorderBrush}"  
              BorderThickness="0" 
              SnapsToDevicePixels="True">
                    <Image Source="{Binding RelativeSource={RelativeSource AncestorType=toolkit:DataGridCell}, Path=Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Note that at the moment i'm binding the Image Source to Content.. which doesnt work, i've also tried Value, which didn't work!

So my question is, nice and simply.. what is the correct binding to use to get the cell's content into the source property of this image?

Thanks in advance!

Pete

+1  A: 

If the column is a DataGridTextColumn then you might be able to bind to the Text property of the TextBlock that is its content:

<Image Source="{Binding RelativeSource=
     {RelativeSource AncestorType=DataGridCell}, Path=Content.Text}" />

That's really a hack, though. If you want to display an image in a column, you should probably use a DataGridTemplateColumn:

<DataGridTemplateColumn Header="...">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding SomeProperty}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Where SomeProperty is the property of your row object that has the image path.

Quartermeister
Thanks for the response, i've ended up opting for the hack to avoid sinking any more time into it!out of curiosity... if I were to use a DataGridTemplateColumn, what would you expect the binding path to be given that the cell is being bound to a cell in a DataTableRow?thanks again!
GreatSeaSpider
@GreatSeaSpider: I think when you bind to a DataTable that you use the DataColumn.ColumnName values as the binding path.
Quartermeister