简体   繁体   中英

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

I am trying to build a program which includes a listview, i have the listview in place but what i want to happen, but isn't, is that when i click on a specific list view item i want the image to change. I am using Visual Studio 2012 and designing for Windows Store NOT the Windows Phone

For example, i have Albert Einstein as the first listview element, when the user clicks on this list view item i would like a picture of Albert Einstein to appear in the image element, vice versa when the user clicks second element in listview an image of Galileo should appear, i have all images pre-loaded

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

This would be the way to display a image, assuming that it is stored in one of your folders, In this examples I'm retrieving it from the assets folder.

 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;

This will create a new bitmapImage as well as creating a new Uri and then sets that 'string' to the baseUri which will represented as the xaml constructed object when at load time

I am setting the bitmap image to the source of my 'image' element within my program I can do this multiple times so new and different URI can be loaded at run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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