WPF DataGrid 绑定 DataGridCell 内容

发布于 2024-09-14 20:28:04 字数 1140 浏览 0 评论 0原文

希望这将是一个非常简单的答案,我认为我只是没有看到众所周知的树木。

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

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

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

提前致谢!

皮特

This is hopefully going to be a really simple answer, I'm just not seeing the proverbial wood for the trees I think.

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?

Thanks in advance!

Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2024-09-21 20:28:04

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

<Image Source="{Binding RelativeSource=
     {RelativeSource AncestorType=DataGridCell}, Path=Content.Text}" />

不过,这确实是一个 hack。如果要在列中显示图像,您可能应该使用 DataGridTemplateColumn

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

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

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}" />

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>

Where SomeProperty is the property of your row object that has the image path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文