2016-09-29 103 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

这将做到这一点,谢谢! –