我想在listview中显示图像但我不知道应该怎么做。
我的Guest类有方法GetGuestImage()
它返回一个绝对路径。
当这个文件存在时,应该加载,当不加载占位符图像时。
我怎么能意识到这一点?
<ListView x:Name="listview">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding GetGuestImage()}" Header="Image" />
<GridViewColumn DisplayMemberBinding="{Binding Vorname}" Header="Vorname" />
<GridViewColumn DisplayMemberBinding="{Binding Nachname}" Header="Nachname" />
<GridViewColumn DisplayMemberBinding="{Binding Postleitzahl}" Header="PLZ" />
<GridViewColumn DisplayMemberBinding="{Binding Ort}" Header="Ort" />
<GridViewColumn DisplayMemberBinding="{Binding Land}" Header="Land" />
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:3)
您可以使用CellTemplate和值转换器完成此操作:
<ListView x:Name="listview" xmlns:local="clr-namespace:WpfApplication1">
<ListView.Resources>
<local:ImageConverter x:Key="ImageConverter" />
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Image">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=., Converter={StaticResource ImageConverter}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Vorname}" Header="Vorname" />
<GridViewColumn DisplayMemberBinding="{Binding Nachname}" Header="Nachname" />
<GridViewColumn DisplayMemberBinding="{Binding Postleitzahl}" Header="PLZ" />
<GridViewColumn DisplayMemberBinding="{Binding Ort}" Header="Ort" />
<GridViewColumn DisplayMemberBinding="{Binding Land}" Header="Land" />
</GridView>
</ListView.View>
</ListView>
namespace WpfApplication1
{
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
dynamic dataObject = value;
if (dataObject != null)
{
string path = dataObject.GetGuestImage();
if(System.IO.File.Exists(path))
return new Uri(dataObject.GetGuestImage(), UriKind.RelativeOrAbsolute);
}
return new Uri(@"c:\yourplaceholderimage.png", UriKind.RelativeOrAbsolute);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
但是,您无法直接绑定到GetGuestImage()方法。另一个想法可能是通过数据对象类的Uri属性公开路径并绑定到此路径。那你就不需要转换器了:
<GridViewColumn Header="Image">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding GuestImage}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
public class YourDataClass
{
public Uri GuestImage
{
get { return new Uri(@"c:\picture.png", UriKind.RelativeOrAbsolute); }
}
//+ the other properties...
}
答案 1 :(得分:1)
使用转换器。
public class NullGuestImageToPlaceholderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || !File.Exists(value as string))
return placeholderImage;
return GetImage(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<ListView x:Name="listview">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding GuestImage, Converter={StaticResource nullGuestImageConverter}}" Header="Image" />
<!-- your other columns -->
</GridView>
</ListView.View>
</ListView>
我没有GetGuestImage实现或完整的XAML,所以我现在能做的最多。
nullGuestImageConverter
应该是转换器的x:Key
,在您的XAML资源中定义(如<UserControl.Resources>
),而placeholderImage
应该是如何实现的你检索所说的占位符图像(我猜想会出现在GetGuestImage()
)。
事实上,我猜您可以简单地用Convert
替换GetGuestImage
方法的内容,但我无法确切知道。
答案 2 :(得分:0)
首先,您需要绑定到属性而不是方法,DisplayMemberBinding仅支持切换到CellTemplate所需的基本绑定
<ListView x:Name="listview">
<ListView.View>
<GridView>
<GridViewColumn Header="Image" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding GuestImageProperty}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Vorname}" Header="Vorname" />
<GridViewColumn DisplayMemberBinding="{Binding Nachname}" Header="Nachname" />
<GridViewColumn DisplayMemberBinding="{Binding Postleitzahl}" Header="PLZ" />
<GridViewColumn DisplayMemberBinding="{Binding Ort}" Header="Ort" />
<GridViewColumn DisplayMemberBinding="{Binding Land}" Header="Land" />
</GridView>
</ListView.View>
</ListView>
然后你可以使用绑定的TargetNullValue和FallbackValue属性来处理缺失值