在 WPF 列表视图中显示图像
Display Image in WPF Listview
我想在我的列表视图中显示一张图片,但我不知道该怎么做。
我的客人 class 有方法 GetGuestImage()
它 returns 我是绝对路径。
当此文件存在时应加载,当不存在时应加载占位符图像。
我如何实现这一点?
<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>
为此使用转换器。
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
方法,但我不能确定。
您可以使用 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() 方法。另一个想法可能是通过数据对象 class 的 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...
}
首先你需要绑定到一个 属性 而不是方法其次 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 属性来处理缺失值
我想在我的列表视图中显示一张图片,但我不知道该怎么做。
我的客人 class 有方法 GetGuestImage()
它 returns 我是绝对路径。
当此文件存在时应加载,当不存在时应加载占位符图像。
我如何实现这一点?
<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>
为此使用转换器。
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
方法,但我不能确定。
您可以使用 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() 方法。另一个想法可能是通过数据对象 class 的 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...
}
首先你需要绑定到一个 属性 而不是方法其次 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 属性来处理缺失值