mercredi 6 mai 2015

wpf copy and replace file doesn't update image in window

I have window to manage the books table, in this window there is a image which has its source property filled with the image name. For that I have this method.

protected void bindImage()
{
    string folderPath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\bookImages\\";
    folderPath += tbLivrosRow.Imagem.ToString();
    BitmapImage bitMapImage = new BitmapImage();
    bitMapImage.BeginInit();
    bitMapImage.UriSource = new Uri(folderPath);
    bitMapImage.CacheOption = BitmapCacheOption.OnLoad;
    imgBookCover.Source = bitMapImage;
    bitMapImage.EndInit();
}

To change the book cover...

private void changeCover(object sender, RoutedEventArgs e)
{
    OpenFileDialog CxDialog = new OpenFileDialog();
    string folderPath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\bookImages\\";
    CxDialog.Title = "Seleccione a capa pretendida";
    CxDialog.Filter = "JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png";
    CxDialog.FilterIndex = 1; // com o valor 1 aparece na caixa de diálogo o primeiro filtro de pesquisa, *.JPG
    CxDialog.CheckFileExists = true;
    bool? myResult;
    myResult = CxDialog.ShowDialog();
    if (myResult != null && myResult == true)
    {
        string filePath = folderPath + tbLivrosRow.Imagem.ToString().Trim();
        System.IO.File.Copy(CxDialog.FileName, filePath, true);
        imgBookCover.Source = null;
        Bind();
    }
}

The Bind() fills some listViews, textboxes, etc. and calls the bindImage().

The image is successfully copied to the destination folder, replaces the existing one, but the image never gets updated in the window.xaml. The image only updates after I exit this window.

I have tried to set the image source property to null but it does not get it done.

Aucun commentaire:

Enregistrer un commentaire