【问题标题】:Displaying an image from resources in a datagrid that is bound to a list of objects在绑定到对象列表的数据网格中显示资源中的图像
【发布时间】:2012-01-17 21:25:41
【问题描述】:

我有一个包含几列的数据网格。其中一列是 templateColumn,我想从我的资源中显示一个图像。这是 xaml:

<DataGrid AutoGenerateColumns="False" Height="415" HorizontalAlignment="Left" Margin="0,34,0,0" Name="dgLocalPlugins" VerticalAlignment="Top" Width="806" SelectionMode="Single" AlternatingRowBackground="#CDEBEBEB">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Enabled" Binding="{Binding Path=Enabled}" />
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" MinWidth="200" />
        <DataGridTextColumn Header="Status" Binding="{Binding Path=Status}" />
        <DataGridTextColumn Header="Version" Binding="{Binding Path=Version}" />
        <DataGridHyperlinkColumn Header="Wiki" Binding="{Binding Path=WikiUrl}" MaxWidth="100" />
        <DataGridTextColumn Header="Author" Binding="{Binding Path=Author}" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Source="{Binding Path=Upgrade}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

这是填充数据网格绑定到的对象列表的代码的核心:

foreach (string path in osapdFiles)
{
    if (!string.IsNullOrEmpty(path))
    {
        PluginDescription desc = PluginHelper.Deserialize(path);
        desc.Upgrade = Properties.Resources.upgrade;
        desc.Status = "Stopped";
        desc.Enabled = false;
        pluginList.Add(desc);
    }
}

dgLocalPlugins.ItemsSource = pluginList;

当我在 desc.Upgrade 设置后调试并设置断点时,我可以看到那里有一些东西,但图像没有显示在数据网格中。我需要做什么才能显示图像?

【问题讨论】:

    标签: c# .net wpf datagrid


    【解决方案1】:

    确保您的 PluginDescription 类实现 INotifyPropertyChanged 以便在设置属性时绑定会更新 UI。

    【讨论】:

    • 我不确定这是问题所在。所有其他列都正确显示。
    • 不,我将该实现添加到 PluginDescription 类中,但它仍然不显示图像。
    【解决方案2】:

    如果您已经为PluginDescription 类的Upgrade 属性正确实现了INotifyPropertyChanged,我想那么Path 本身并没有得到解决。

    将第一个PluginDescription的路径放入数据网格外的某个图像中。

      <DataGrid Name="dgLocalPlugins" ... />
      <Image Source="{Binding ElementName=dgLocalPlugins, Path=ItemsSource[0].Upgrade}"/>
    

    并检查此单个图像是否正确加载。如果没有,那么我确定路径没有得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多