2010-08-17 113 views
4

这是希望将是一个非常简单的答案,我只是没有看到树木我认为的谚语木材。WPF DataGrid绑定DataGridCell内容

我有在我想的细胞图像的源属性的内容绑定DataGridCell风格,这里是我用此刻的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> 

注意此刻,我将图像源绑定到内容..这不起作用,我也尝试过价值,这不起作用!

所以我的问题是,很好,很简单..什么是正确的绑定来获取单元格的内容到这个图像的源属性?

在此先感谢!

皮特

回答

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是具有图像路径的行对象的属性。

+0

感谢您的回应,我最终选择了黑客,以避免再陷入更多时间! 出于好奇...如果我要使用DataGridTemplateColumn,你会期望绑定路径被赋予单元格绑定到DataTableRow中的单元格吗? 再次感谢! – GreatSeaSpider 2010-08-17 13:08:04

+0

@GreatSeaSpider:我认为当你绑定到一个DataTable时,你使用DataColumn.ColumnName值作为绑定路径。 – Quartermeister 2010-08-17 13:27:42