WPF DataGrid绑定DataGridCell内容

时间:2010-08-17 12:06:29

标签: c# wpf data-binding 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>

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

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

提前致谢!

皮特

1 个答案:

答案 0 :(得分:6)

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

<Image Source="{Binding RelativeSource=
     {RelativeSource AncestorType=DataGridCell}, Path=Content.Text}" />
但是,这真的是一个黑客攻击。如果要在列中显示图像,则应该使用DataGridTemplateColumn

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

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

相关问题