【问题标题】:WPF DataGrid Binding DataGridCell ContentWPF DataGrid 绑定 DataGridCell 内容
【发布时间】:2011-03-30 22:51:30
【问题描述】:

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

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

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

所以我的问题是,简单明了.. 将单元格的内容放入此图像的源属性的正确绑定是什么?

提前致谢!

皮特

【问题讨论】:

    标签: c# wpf data-binding datagrid datagridcell


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,我最终选择了 hack 以避免陷入更多时间!出于好奇...如果我要使用 DataGridTemplateColumn,如果单元格被绑定到 DataTableRow 中的单元格,您希望绑定路径是什么?再次感谢!
    • @GreatSeaSpider:我认为当您绑定到 DataTable 时,您使用 DataColumn.ColumnName 值作为绑定路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多