【问题标题】:WPF ListView binding image from resource, dynamic nameWPF ListView 从资源绑定图像,动态名称
【发布时间】:2015-01-20 01:51:45
【问题描述】:

假设我在我的视图模型中有一个人员列表:

public ObservableCollection<Person> Persons

Person 类是:

public class Person
{
    public string Name{ get; set; }
    public string Photo { get; set; }
}

我想在 ListView 中显示人员 到目前为止,一切都很好。 我有一组照片图像。 我的问题是:

  1. 我应该将这些照片作为资源添加到项目的“属性->资源”选项卡中还是将“构建操作”设置为“资源”的项目文件夹中?
  2. 如何将 Person Photo 属性绑定到相应的照片资源(假设照片图像名称与 Person Photo 属性相同)?

这是我的 XAML 代码,但绑定当然不起作用:

    <ListView Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Persons}" Margin="4">
        <ListView.View>
            <GridView ColumnHeaderToolTip="Persons Information">
                <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
                <GridViewColumn Header="Photo" Width="60">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Photo}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

提前致谢。

【问题讨论】:

  • 有什么建议吗?
  • 那么,这不可能吗?
  • 过几天我会修复你的:-)
  • 对不起,我没听懂。

标签: wpf image listview binding resources


【解决方案1】:

我终于通过在创建 Person 对象时设置完整的资源路径解决了我的问题,即: Photo = @"Resources\" + photoName + ".bmp".

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多