2016-09-29 105 views
2

我有一個WPF窗口列表視圖如下更改圖像源

<ListView Name="lvInstructors" ItemsSource="{Binding Instructors}"> 
        <ListView.ItemsPanel> 
         <ItemsPanelTemplate> 
          <UniformGrid Columns="3"> 
          </UniformGrid> 
         </ItemsPanelTemplate> 
        </ListView.ItemsPanel> 

        <ListView.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <StackPanel Orientation="Horizontal"> 
            <Image Source="http://localhost:30870/Content/img/avatar1.jpg" Width="30px"></Image> 
            <TextBlock Text="{Binding InstructorName}" FontWeight="Bold" /> 
           </StackPanel> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Text="{Binding Description}" /> 
           </StackPanel> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Text="{Binding Qualifications}" /> 
           </StackPanel> 
           <StackPanel Orientation="Horizontal" Name="InstructorRating"> 
            <TextBlock Text="{Binding Rating}" /> 
           </StackPanel> 
          </Grid> 
         </DataTemplate> 
        </ListView.ItemTemplate> 
       </ListView> 

我需要改變圖像源屬性,它位於第一StackPanel的內部程序,我該怎麼做?

+0

爲什麼不將它綁定到教師的圖像或圖像來源屬性?通過這種方式,您可以爲您的教師列表中的每位教師獲取頭像/圖像。 – Xeun

+0

我需要從遠程服務獲取每個圖像 –

+0

這不是一個真正的原因,爲什麼你不應該通過綁定來做到這一點。您可能正在查詢教師的服務,無論您是在那裏獲取圖像還是源代碼路徑。或者你正在圖像屬性Getter(我不會建議) – Xeun

回答

0

考慮到你的XAML的代碼:

<ListView Name="lvInstructors" ItemsSource="{Binding Instructors}"> 
       <ListView.ItemsPanel> 
        <ItemsPanelTemplate> 
         <UniformGrid Columns="3"> 
         </UniformGrid> 
        </ItemsPanelTemplate> 
       </ListView.ItemsPanel> 

       <ListView.ItemTemplate> 
        <DataTemplate> 
         <Grid> 
          <StackPanel Orientation="Horizontal"> 
           <Image Source="{Binding ImageSource}" Width="30px"></Image> 
           <TextBlock Text="{Binding InstructorName}" FontWeight="Bold" /> 
          </StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding Description}" /> 
          </StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding Qualifications}" /> 
          </StackPanel> 
          <StackPanel Orientation="Horizontal" Name="InstructorRating"> 
           <TextBlock Text="{Binding Rating}" /> 
          </StackPanel> 
         </Grid> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 

現在,因爲我們不知道你的代碼是如何工作的,我將只是張貼片段:

public class ViewModel 
{ 
    public ObservableCollection<Instructor> Instructors {get; set} 

    public void RetrieveInstructorsFromService() 
    { 
     // the service needs to populate all properties of the Instructor isntance. 
     var instructors = service.GetInstructors(); 
     this.Instructors = new ObservableCollection<Instructor>(instructors); 
    } 
} 

public class Instructor 
{ 
    [...] 
    public string Description { get; set; } 
    public string ImageSource { get; set; } 
    [...] 
} 

現在你的列表將顯示所有教師該服務與他們相應的頭像圖像。

+0

這將做到這一點,謝謝! –