WPF DataGrid绑定DataGridCell内容 [英] WPF DataGrid Binding DataGridCell Content

查看:402
本文介绍了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.

这篇关于WPF DataGrid绑定DataGridCell内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆