WPF DataGrid绑定DataGridCell内容

Gre*_*der 4 c# data-binding wpf datagrid datagridcell

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,目前我正在将图像源绑定到内容..这不起作用,我也尝试过Value,这不起作用!

所以我的问题是,很好,很简单..用于将单元格的内容放入此图像的source属性的正确绑定是什么?

提前致谢!

皮特

Qua*_*ter 6

如果列是DataGridTextColumn,那么您可能能够绑定到TextBlock的Text属性,即其内容:

<Image Source="{Binding RelativeSource=
     {RelativeSource AncestorType=DataGridCell}, Path=Content.Text}" />
Run Code Online (Sandbox Code Playgroud)

但这确实是一个黑客攻击.如果要在列中显示图像,则应该使用DataGridTemplateColumn:

<DataGridTemplateColumn Header="...">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding SomeProperty}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

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