简体   繁体   English

如何在ListView中显示实际图像而不是它的位置?

[英]How to display an actual image in ListView rather than its location?

I have found this code to add picture to a ListView in WPF. 我发现此代码将图片添加到WPF中的ListView。 However it only displays the image location rather than the actual image: 但是它只显示图像位置而不是实际图像:

            var image = new BitmapImage();

            string fileName = @"C:\Peak Sourcing\Work\ppt_test\slides_png\slide1.png";

            using (var stream = new FileStream(fileName, FileMode.Open))
            {
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = stream;
                image.EndInit();
            }


            ListBoxItem item = new ListBoxItem();
            Thumbnails.Items.Add(image);

The XAML behind is simply: 背后的XAML很简单:

<ListView Name="Thumbnails" Margin="10,10,804,10" >
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="1"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>



 </ListView>

I also found few other codes, but all of them just show the image location as string rather than image. 我还发现其他几个代码,但它们只是将图像位置显示为字符串而不是图像。 Can you please help me solve this? 你能帮我解决一下吗?

This can be added to the XAML : 这可以添加到XAML:

        <ListView  x:Name="lv" Background="WhiteSmoke" 
                   Height="500" 
                   ItemsSource="{Binding models}">
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding ImagePath}" 
                                       HorizontalAlignment="Left" 
                                       Stretch="Fill" 
                                       Height="100"
                                       Width="100"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

Yow need a model for this : 你需要一个模型:

public class Model : INotifyPropertyChanged
{
private Uri _ImagePath;
public Uri ImagePath
{
    get
    {
        return _ImagePath;
    }
    set
    {
        _ImagePath = value;

        PropertyChanged(this, new PropertyChangedEventArgs("ImagePath"));
    }
}

public event PropertyChangedEventHandler PropertyChanged = delegate { };

} }

And an example for the code behind: 以及代码背后的示例:

public partial class MainWindow : Window
{
public Model ImageModel { get; set; }

public MainWindow()
{
    InitializeComponent();

    ImageModel = new Model();
    ImageModel.ImagePath = new Uri(@"/ImageSource;component/Images/Image1.jpg", UriKind.RelativeOrAbsolute);
    this.DataContext = ImageModel;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    ImageModel.ImagePath = new Uri(@"/ImageSource;component/Images/Image2.jpg", UriKind.RelativeOrAbsolute);
}

} }

This is the easiest and fastest way to tackle this issue. 这是解决此问题的最简单,最快捷的方法。 If you need MVVM just raise a hand and we can get a Command instead of that Click event. 如果你需要MVVM只需举手,我们就可以得到一个Command而不是那个Click事件。 One more thing, do you have a list of images? 还有一件事,你有一个图像列表吗? If so, we will have to use an ObservableCollection and instantiate many models in order to feed all the Buttons. 如果是这样,我们将不得不使用ObservableCollection并实例化许多模型以便为所有按钮提供信息。

  • ImageSource is my assembly name. ImageSource是我的程序集名称。

  • Images is a folder created in my project. 图像是在我的项目中创建的文件夹。

In order to populate the ListView you need an ObservableCollection<Model> defined like this : 为了填充ListView,您需要一个像这样定义的ObservableCollection<Model>

      public ObservableCollection<Model> models { get; set; }

Initialize it in the constructor : 在构造函数中初始化它:

      models = new ObservableCollection<Model>();

and add some Model instances to it with their image path set as shown above. 并向其添加一些Model实例,其图像路径设置如上所示。

You are missing a DataTemplate that actually displays the BitmapImage objects: 您缺少一个实际显示BitmapImage对象的DataTemplate:

<ListView Name="Thumbnails" ... >
    ...
    <ListView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Without the DataTemplate a ListViewItem just shows the output of the ToString() method of the data items. 如果没有DataTemplate,ListViewItem只显示数据项的ToString()方法的输出。


Then (without any MVVM) an easy way to show all PNG files in a folder would be this: 然后(没有任何MVVM)一个简单的方法来显示文件夹中的所有PNG文件将是这样的:

Thumbnails.ItemsSource = Directory
    .EnumerateFiles(@"C:\Peak Sourcing\Work\ppt_test\slides_png", "*.png")
    .Select(f => new BitmapImage(new Uri(f)));

You are adding BitmapImage as ListViewItem , which is not visual (so it doesn't renders) and displayed as ToString() . 您正在将BitmapImage添加为ListViewItem ,它不是可视的(因此它不呈现)并显示为ToString()

Normally you should use DataTemplates in such cases, but this should works for you too: 通常你应该在这种情况下使用DataTemplates ,但这也适用于你:

var fileName = @"C:\Peak Sourcing\Work\ppt_test\slides_png\slide1.png";
var bitmap = new BitmapImage(new Uri(fileName)) { /*.. more bitmap image options if you like .. */ };
var image = new Image() { Source = bitmap /*.. more image options ..*/ }
Thumbnails.Items.Add(image);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问RadGrid DataItem的实际值而不是其文本 - Accessing the actual value of a RadGrid DataItem rather than its text 如何在WPF,C#中围绕其中心而不是其上的画布旋转图像 - How to rotate image about its centre rather than about the canvas it is on in WPF, C# 图像相对于原始位置而不是窗口移动 - Image moves relative to original location rather than the window 为什么在粘贴到 Excel 时图像显示的大小大于其实际大小,我如何才能使其以自然大小显示? - Why does an image display at greater that its actual size when pasting into Excel, and how can I get it to display at its natural size? C# 服务集合将类型添加为对象而不是其实际类型 - C# Service Collection adds type as object rather than its actual type 下载动态生成的图像,而不是在浏览器中显示 - Download an image generated dynamically rather than display in browser ASP.NET MVC下载图像而不是在浏览器中显示 - ASP.NET MVC download image rather than display in browser 如何获取datagridView显示列表的内容而不只是长度 - how to get datagridView display content of list rather than just length C#采取介绍网站的html代码而不是实际的代码,如何跳过这一点? - C# Taking the html code of introduction site rather than the actual one, how to skip this? 如何引用实际类中的枚举而不是C#中的基类 - How to refer to an enum in the actual class rather than the base class in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM