尝试通过Image.Source和ImagePath
将我的BusinessLineList
字符串绑定到名为UriSource
的List中的image元素。当我在XAML中硬编码一个图像路径时,它可以工作,但不能绑定到ImagePath
属性。
以下是模型:
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;
}
}
这是ViewModel代码:
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"/>
这是XAML中的ItemTemplate:
<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>
答案 0 :(得分:1)
首先,像这样添加ms-appx:///
"ms-appx:///Assets/lifebuoy2.png"
第二,您可以直接设置ImageSource
:
<Image Source="{Binding ImagePath}"/>