WPF DataGrid绑定DataGridCell内容

问题描述:

希望这将是一个非常简单的答案,我只是没有看到我认为的树木所用的谚语。

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

我有一个DataGridCell我想将单元格的内容绑定到图像的source属性的样式,这是我目前正在使用的XAML:

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?

预先感谢!

Pete

如果该列是DataGridTextColumn,则可以绑定到其内容的TextBlock的Text属性:

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}" />

但这确实是一个hack。如果要在列中显示图像,则可能应使用 DataGridTemplateColumn

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>

其中SomeProperty是具有图像路径的行对象的属性。

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