繁体   English   中英

通过单击Visual Studio 2012 c#和xaml中的列表视图项来更改图像

[英]Change image by click of list view item in Visual Studio 2012 c# and xaml

我正在尝试构建一个包含列表视图的程序,我具有适当的列表视图,但是我想发生的事情不是,而是当我单击特定的列表视图项时,我希望图像发生更改。 我正在使用Visual Studio 2012并为Windows应用商店而非Windows Phone设计

例如,我将Albert Einstein作为第一个listview元素,当用户单击此列表视图项时,我希望Albert Einstein的图片出现在图像元素中,反之亦然,当用户单击listview中的第二个元素时,伽利略应该出现,我已经预装了所有图像

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ScientistListView.SelectedIndex==0)
    {
        ImageForScience.Source = new ImageSource(new Uri("Benjamin_Franklin.png", UriKind.Absolute));
        textBelowHistory.Text = @"Einstein was born in 1879 in Germany (Ulm, Wuttemberg) and died on April 18 1955 
        He developed the special and general theories of relativity

        While working in a patent office Einstein had time to work on his theories of relativity
        In 1915 Einstein completed his general theory of relativity

假设图像存储在您的一个文件夹中,这将是一种显示图像的方式。在本示例中,我将从资产文件夹中检索图像。

 string path = "/Assets/none.jpg";
                Uri uriPath = new Uri(path, UriKind.Relative);
                BitmapImage image1 = new BitmapImage(uriPath);
                Picturebox1.Source = image1; 
 BitmapImage ImageName= new BitmapImage(new Uri( this.BaseUri,"Assets/PicturName.png"));

    ImageForScience.Source = ImageName;

这将创建一个新的bitmapImage以及一个新的Uri,然后将该“字符串”设置为baseUri,该基类在加载时将表示为xaml构造的对象

我将位图图像设置为程序中“图像”元素的源,我可以多次执行此操作,因此可以在运行时加载新的和不同的URI。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM