WPF DataGrid Binding DataGridCell Content

This, I hope, will be a very simple answer, I just do not see the notorious tree for the trees that I think.

I have a DataGridCell style in which I want to associate the contents of a cell with the original image property, here is the XAML that I am currently using:

<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> 

Please note that at the moment I am linking the image source to the content .. which does not work, I also tried a value that did not work!

So my question is nice and simple .. What is the correct binding used to get the contents of the cell in the original property of this image?

Thanks in advance!

Pete

+4
source share
1 answer

If the column is a DataGridTextColumn, then you can bind to the Text Text Text property, which is its contents:

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

It really hack. If you want to display the image in a column, you probably should use a DataGridTemplateColumn :

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

Where SomeProperty is a property of your string object that has a path to the image.

+6
source

All Articles