【问题标题】:WPF ListView show ImageWPF ListView 显示图像
【发布时间】:2024-04-24 23:15:01
【问题描述】:

我有一个简单的类,它包含一个字符串和一个 WPF Image 控件:

public class User
{
    public string Name { get; set; }

    public System.Windows.Controls.Image Image { get; set; }
}

现在我将这个类的实例列表绑定到ListView

我现在想做的是让ListView 显示 Image 属性。

我尝试将 DisplayMemberPath 设置为 Image,但这显示的是 ImageToString 值 (System.Windows.Controls.Image)。

如何让它真正显示控件?

这是 XAML 代码:

<ListView Name="listView1" DisplayMemberPath="Image" />

谢谢!

【问题讨论】:

    标签: wpf image listview controls bind


    【解决方案1】:

    您可以创建一个使用ContentPresenterDataTemplate 来显示图像:

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Image}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

    • 啊,是的!这就是我要找的:) 谢谢!我确实尝试过使用 ListViewItem,但这在选择项目时会以某种方式引入奇怪的行为。