I have a wpf application that has a button for for each folder under a certain path on the users hard drive. Each folder contains an image that is shown on the button and a file that is run when the button is clicked. Here is the template that I am using for the button:
<DataTemplate x:Key="ProgramItemDataTemplate">
<Button Style="{StaticResource ButtonStyle}" Click="Program_Click" Tag="{Binding Key}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem x:Name="DeleteMenuItem" Click="DeleteMenuItem_Click" Header="Delete" Tag="{Binding Key}" />
</ContextMenu>
</Button.ContextMenu>
<StackPanel>
<Image Source="{Binding Value}" MaxWidth="200" MaxHeight="175"></Image>
<TextBlock Text="{Binding Key,Converter={StaticResource PathToNameConverter2}}" TextWrapping="Wrap" TextAlignment="Center" />
</StackPanel>
</Button>
</DataTemplate>
The binding value is the path to the image and the binding key is the path to another file that is run when the button is clicked. The problem is the DeleteMenuItem function. I want to delete the folder that contains the image but it won’t let me because the image file is in use by the button. How can I release the image from use by my application so that I can safely delete the folder?
By default BitmapImage’s BitmapCacheOption is
OnDemandyou can change this toOnLoadby having your ownValueConverterand that should solve your issue.