Windows phone 7 如何在wp7中动态绑定列表框中的图像源?
霉菌代码:Windows phone 7 如何在wp7中动态绑定列表框中的图像源?,windows-phone-7,Windows Phone 7,霉菌代码: 利息=(来自xmlDocu.substands(“朋友”)中的rts) 选择new SampleCheckedData { Id=(字符串)rts.Element(“userid”), 名称=(字符串)rts.Element(“名称”), Icon=(字符串)rts.Element(“imageurl”), VisibleStatus=(字符串)rts.Element(“VisibleStatus”), AppStatus=(字符串)rts.Element(“loginstatus
利息=(来自xmlDocu.substands(“朋友”)中的rts)
选择new SampleCheckedData
{
Id=(字符串)rts.Element(“userid”),
名称=(字符串)rts.Element(“名称”),
Icon=(字符串)rts.Element(“imageurl”),
VisibleStatus=(字符串)rts.Element(“VisibleStatus”),
AppStatus=(字符串)rts.Element(“loginstatus”),
}).ToList();
this.lstImages.ItemsSource=利率;
我能够正确绑定名称和图标。但我需要将Imagesrc image设置为visiblestatus,Appstatus booth为true,然后需要绑定一种类型的映像,否则需要通过详细代码绑定另一种类型的映像。如何实现此目的?只需使用名为Imagesrc的属性,如下所示:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Height="100" Width="105" Source="{Binding Icon}" VerticalAlignment="Top" OpacityMask="White" Stretch="Fill" />
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextSubtleStyle}"
Width="110" TextAlignment="Center"/>
<Image Source="{Binding ImageSrc}" Height="20" Width="20" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-50,15,0" Stretch="Fill" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
}).ToList<SampleCheckedData>();
this.lstImages.ItemsSource = interestrates;
它应该会起作用。至少这是我用来绑定图像的方式
注意:我假设Icon属性具有图像的绝对路径。只需添加到@josemiguel.torres,image.Source的类型为ImageSource,因此您不需要返回Uri,而需要执行以下操作:
public class SampleCheckedData
{
//... your properties
public Uri ImageSrc
{
get
{
if ((bool.Parse(this.VisibleStatus) && (bool.Parse(this.AppStatus))
{
if (!string.IsNullOrEmpty(this.Icon))
return new Uri(this.Icon, UriKind.Absolute); //or whatever image
}
else
{
return new Uri("/Images/YourOtherImage.png", UriKind.Relative);
}
}
}
}
现在出了什么问题?您不能只绑定到包含逻辑的属性来确定要显示的图像吗?
return new BitmapImage(new Uri(this.Icon, UriKind.Absolute));