【问题标题】:Set dynamic images to Autogenerated columns in WPF Datagrid在 WPF Datagrid 中将动态图像设置为自动生成的列
【发布时间】:2013-03-24 20:01:18
【问题描述】:

DataGrid 是这样声明的

<DataGrid x:Name="DgDirectories" 
                  AutoGenerateColumns="True" />

它是使用DgDirectories.ItemSource 属性和我的 DTO 对象的IEnumerable 动态填充的。该对象包含带有 dynamic 图像的 byte[] 属性(HDD 上不存在)。 我想显示那些网格中的图像

我尝试将 byte[] 转换为 BitmapImage 并将其与数据一起传递,但无论如何网格都会为这些类型创建 DataGridTextColumn

也许这可以更容易地完成,但我有一点使用 WPF 的经验...... 请帮我在列中显示图像

P.S.:我是 WPF 的新手。

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    动态路径名绑定的 Xaml 代码

    <DataGrid x:Name="dataGrid1" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding ImageFilePath}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    现在只需使用通过 IEnumerable 文件路径列表实例化的 uri 对象开始您的逻辑背后的代码... 希望这就够了

    【讨论】:

    • 说实话,我还没有尝试过您的代码 - 我能够重新设计应用程序的该部分以使用具有指定列的数据网格。但是我会将您的帖子标记为答案-希望这有效:)。当网格将包含其他列而不是日期时间时,它是否有效?
    【解决方案2】:

    您应该删除 autogeneratecolumn=true 并指定要显示的列。这将使您有机会指定具有图像的列的数据模板。

    【讨论】:

    • 这对我不起作用 - 我需要自动生成列,因为我在网格中显示了几种不同类型的对象。如果我指定列,我只能对一种类型的对象执行此操作,并且当 ItemsSource 更改时,映射将不再起作用。有没有办法可以指定多个可选映射,具体取决于 ItemsSource 属性中的对象?
    猜你喜欢
    • 2012-04-24
    • 2010-12-31
    • 2017-10-23
    • 2014-03-12
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多