首页 文章

WPF DataGrid绑定DataGridCell内容

提问于
浏览
4

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

我有一个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 回答

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

相关问题