I am trying to load a few hundred images in a listview without locking the image file. I want to be able to delete the file while the program is running. I created a custom image converter and bind it to the listview.
However, the code is not compiling and I am getting 'The name "ImageConverter" does not exist in the namespace "clr-namespace:CoolApp.ImageConverter'
What am I doing wrong?
XAML:
<Window x:Class="CoolApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CoolApp"
xmlns:c="clr-namespace:CoolApp.ImageConverter"
mc:Ignorable="d"
Title="MainWindow" MinHeight="600" Height="600" MinWidth="800" Width="800"
Background="#FF222431" WindowStartupLocation="CenterScreen">
<Window.Resources>
<c:ImageConverter x:Key="ImgConverter" />
</Window.Resources>
<Grid Name="ThumbGrid" Grid.Row="1" Margin="10,10,0,0" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListView x:Name="Thumbview" Grid.Row="0" BorderThickness="1" IsTextSearchEnabled="True" TextSearch.TextPath="{Binding title}" VirtualizingPanel.IsVirtualizingWhenGrouping="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="{x:Null}" BorderBrush="{x:Null}" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition />
</Grid.RowDefinitions>
<Image Name="Thumbnails" Grid.Row="0" Source="{Binding picPath, Converter={StaticResource ImgConverter}, IsAsync=True}" Stretch="Uniform" Height="255" Width="170" ToolTip="{Binding title}" HorizontalAlignment="Center" VerticalAlignment="Center">
</Image>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
</Window>
C#:
namespace CoolApp
{
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var image = new BitmapImage();
image.BeginInit();
image.UriSource = value is Uri ? (Uri)value : new Uri(value.ToString());
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
return image;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//code to add images to ItemsSource
}
Your ImageConverter
is defined inside CoolApp
namespace while you are telling the compiler to look for ImageConverter
inside CoolApp.ImageConverter
namespace, because of a wrong namespace mapping in your xaml.
Your namespace mapping should be like this:
xmlns:c="clr-namespace:CoolApp"