2016-08-01 87 views
0

試圖通過Image.Source和UriSource將我的ImagePath字符串綁定到名爲BusinessLineList的圖像元素。當我在XAML中硬編碼一個圖像路徑時,它可以工作,但不會綁定到ImagePath屬性。將圖像路徑綁定到UWP ListView項目中的圖像

這裏的模型:

public class BusinessLinesModel 

    { 
     public string ImagePath { get; } 
     public string ProductType { get; } 
     public int NumPending { get; } 
     public double DollarAmount { get; } 
     public int NumInforceYTD { get; } 
     public int ID { get; } 


     public BusinessLinesModel(int id, string imagePath, string productType, int numPending, double dollarAmount, int numInforceYTD) 
     { 
      ID = id; 
      ImagePath = imagePath; 
      ProductType = productType; 
      NumPending = numPending; 
      DollarAmount = dollarAmount; 
      NumInforceYTD = numInforceYTD; 
     } 

    } 

這裏的視圖模型代碼:

private AgentsModel _selectedAgent; 
    public AgentsModel SelectedAgent 
    { 
     get { return _selectedAgent; } 
     set 
     { 

      _selectedAgent = value; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedAgent))); 

      IsAgentSelected = Visibility.Visible; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsAgentSelected))); 


      int i = SelectedAgent.ID; 

      BusinessLineList = new List<BusinessLinesModel>(); 

      BusinessLineList.Add(new BusinessLinesModel(i, "Assets/lifebuoy2.png", "Life", i * i + i, i * 83.22, i * 7 + 2)); 
      BusinessLineList.Add(new BusinessLinesModel(i, "Assets/get-money.png", "Annuities", i * i + i, i * 83.22, i * 7 + 2)); 
      BusinessLineList.Add(new BusinessLinesModel(i, "Assets/old-man-walking-with-a-crutch.png", "LTC", i * i + i, i * 83.22, i * 7 + 2)); 
      BusinessLineList.Add(new BusinessLinesModel(i, "Assets/wheelchair2.png", "Disability", i * i + i, i * 83.22, i * 7 + 2)); 
      BusinessLineList.Add(new BusinessLinesModel(i, "Assets/link-symbol.png", "Linked Benefit", i * i + i, i * 83.22, i * 7 + 2)); 

      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BusinessLineList))); 


      IsBusinessLineSelected = Visibility.Collapsed; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsBusinessLineSelected))); 
     } 
    } 

這裏的XAML:

      <ListView x:Name="listView1" ItemTemplate="{StaticResource CasesListModelTemplate}" ItemsSource="{Binding BusinessLineList}" SelectedItem="{Binding SelectedBusinessLine, Mode=TwoWay}" Visibility="{Binding IsAgentSelected, Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Auto"/> 

而這裏的ItemTemplate中的XAML:

<DataTemplate x:Key="CasesListModelTemplate"> 
     <Grid Margin="10" Width="290" Height="100" > 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Image Margin="0,10,0,0" d:LayoutOverrides="LeftPosition, RightPosition, TopPosition, BottomPosition" Width="75" Height="75" HorizontalAlignment="Center" VerticalAlignment="Top"> 
       <Image.Source> 
        <BitmapImage UriSource="{Binding ImagePath}"/> 
        <!--Hard-coding this in line above works: "Assets/lifebuoy2.png"--> 
       </Image.Source> 
      </Image> 
      <   </Grid> 
    </DataTemplate> 

回答

1

起初,加ms-appx:///這樣

"ms-appx:///Assets/lifebuoy2.png" 

在第二,你可以設置ImageSource直接:

<Image Source="{Binding ImagePath}"/> 
+0

感謝 - 你的第一個建議定了!你能解釋一下''ms-appx:///「'是什麼嗎? (我似乎無法讓你的第二個答案工作。) –

+1

關於'ms-appx',你可以在這裏找到更多信息[https://msdn.microsoft.com/library/windows/apps/br242760] –