簡體   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