giovedì 14 novembre 2013

WPF - DataGrid non aggiorna la variabile Binding C#

Mi è capitato di avere il seguente problema:
Ho una DataGrid  che binda ad una Collection, ma modificando un parametro questo non viene aggiornato nella collection. Casualmente mi sono imbattuto nel seguente consiglio:
http://stackoverflow.com/questions/17211462/wpf-bound-datagrid-does-not-update-items-properties 


La soluzione è molto semplice ed è quella di far ereditare alla classe degli item la
ObservableCollection

Ecco l'esempio:

Code:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; // ObservableCollection
using System.ComponentModel; // INotifyPropertyChanged
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObservableCollectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // ATTN: Please note it's a "TrulyObservableCollection" that's instantiated. Otherwise, "Trades[0].Qty = 999" will NOT trigger event handler "Trades_CollectionChanged" in main.
            // REF: http://stackoverflow.com/questions/8490533/notify-observablecollection-when-item-changes
            TrulyObservableCollection<Trade> Trades = new TrulyObservableCollection<Trade>();
            Trades.Add(new Trade { Symbol = "APPL", Qty = 123 });
            Trades.Add(new Trade { Symbol = "IBM", Qty = 456});
            Trades.Add(new Trade { Symbol = "CSCO", Qty = 789 });

        Trades.CollectionChanged += Trades_CollectionChanged;
        Trades.ItemPropertyChanged += PropertyChangedHandler;
        Trades.RemoveAt(2);

        Trades[0].Qty = 999;

        Console.WriteLine("Hit any key to exit");
        Console.ReadLine();

        return;
    }

    static void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
    {
        Console.WriteLine(DateTime.Now.ToString() + ", Property changed: " + e.PropertyName + ", Symbol: " + ((Trade) sender).Symbol + ", Qty: " + ((Trade) sender).Qty);
        return;
    }

    static void Trades_CollectionChanged(object sender, EventArgs e)
    {
        Console.WriteLine(DateTime.Now.ToString() + ", Collection changed");
        return;
    }
}

#region TrulyObservableCollection
public class TrulyObservableCollection<T> : ObservableCollection<T>
    where T : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler ItemPropertyChanged;

    public TrulyObservableCollection()
        : base()
    {
        CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);
    }

    void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Object item in e.NewItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
        if (e.OldItems != null)
        {
            foreach (Object item in e.OldItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(a);

        if (ItemPropertyChanged != null)
        {
            ItemPropertyChanged(sender, e);
        }
    }
}
#endregion

#region Sample entity
class Trade : INotifyPropertyChanged
{
    protected string _Symbol;
    protected int _Qty = 0;
    protected DateTime _OrderPlaced = DateTime.Now;

    public DateTime OrderPlaced
    {
        get { return _OrderPlaced; }
    }

    public string Symbol
    {
        get
        {
            return _Symbol;
        }
        set
        {
            _Symbol = value;
            NotifyPropertyChanged("Symbol");
        }
    }

    public int Qty
    {
        get
        {
            return _Qty;
        }
        set
        {
            _Qty = value;
            NotifyPropertyChanged("Qty");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
#endregion
}

mercoledì 16 ottobre 2013

Concatenare immagini Bitmap C#

Ecco un esempio di codice per concatenare tra loro 2 o più immagini:


Code:
//Concatenate Image in Horizontal
private Bitmap ConcatenateImage(IList<Bitmap> ListImage)
{
 int ImWidth = 0;

 for (int i = 0; i < ListImage.Count(); i++)
 {
  ImWidth = ImWidth + ListImage[i].Width;
 }

 Bitmap bmpfinal = new Bitmap(ImWidth, ListImage[0].Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

 Graphics gr = Graphics.FromImage(bmpfinal);

 gr.DrawImage(ListImage[0], new System.DrawingPoint(0, 0));
 for (int i = 1; i < ListImage.Count(); i++)
 {
  gr.DrawImage(ListImage[i], new System.DrawingPoint(ListImage[i - 1].Width, 0));
 }

 gr.Dispose();
 
 return bmpfinal;
}<!--[if IE]>

Convertire immagini Bitmap in ImageSource

Il seguente codice converte un immagine in formato Bitmap in formato ImageSource:



Code:
//Convert Bitmap to ImageSource
public ImageSource ToImageSource(System.DrawingBitmap source)
{
 BitmapSource bitSrc = null;

 var hBitmap = source.GetHbitmap();

 try
 {
  bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
   hBitmap,
   IntPtr.Zero,
   Int32Rect.Empty,
   BitmapSizeOptions.FromEmptyOptions());
 }
 catch
 {
  bitSrc = null;
 }
 finally
 {
  NativeMethods.DeleteObject(hBitmap);
 }

 return bitSrc;
}

//Class Utility
internal static class NativeMethods
{
 [DllImport("gdi32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 internal static extern bool DeleteObject(IntPtr hObject);
}

Cosa molto importante è la cancellazione dell'oggetto tramite la classe "NativeMethods" altrimenti potreste avere problemi di memoria.

martedì 8 ottobre 2013

Convertire codice C# per inserimento in blog

Ciao a tutti, ho trovato un ottimo sito internet che copiando il frammento di codice c# da inserire in un blog te lo trasforma in codice HTML da inserire facilmente in un blog.

Il sito internet è: http://formatmycsharpcode.blogspot.it/

Aggiornamento interfaccia grafica C# da thread secondario

Aggiornamento interfaccia grafica Windows form da un thread secondario:


Code:
void ThreadSecondario(object sender, EventArgs e)
{
 if (this.InvokeRequired)
 {
  Invoke(new MethodInvoker(delegate() { ThreadSecondario(sender, e); }));
 }
 else
 {
  AggiornaValori(sender);
 }
}

void AggiornaValori(object sender)
{
 //Aggiornamento valori form
}

Aggiornamento interfaccia grafica WPF da un thread secondario:


Code:
using System.Windows.Threading;

private delegate void AggiornaValoriDelegate(object sender);
void ThreadSecondario(object sender, EventArgs e)
{
 Dispatcher.BeginInvoke(DispatcherPriority.Normal, new AggiornaValoriDelegate(AggiornaValori), sender);
}
void AggiornaValori(object sender)
{
 //Aggiornamento valori controlli WPF
}

venerdì 10 maggio 2013

Trasformazione immagini in Base64 (C#)

La trasformazione Base64 non fa altro che trasformare un immagine in una stringa che può essere facilmente memorizzato in un Database. Di seguito una piccola funzione in C# che a partire dal Path del file restituisce una stringa contenente l'immagine codificata: 

Code:
        private string GenerateBase64(string PathFile)
        {
            string StrBase64 = null;

            System.IOFileStream inFile;
            byte[] binaryData;

            try
            {
                inFile = new System.IOFileStream(PathFile,
                                          System.IO.FileMode.Open,
                                          System.IO.FileAccess.Read);
                binaryData = new Byte[inFile.Length];
                long bytesRead = inFile.Read(binaryData, 0,
                                     (int)inFile.Length);
                inFile.Close();
            }
            catch (SystemException exp)
            {
                throw new Exception(exp.Message);
            }

            // Convert the binary input into Base64 UUEncoded output.
            try
            {
                StrBase64 =
                  System.Convert.ToBase64String(binaryData,
                                         0,
                                         binaryData.Length);
            }
            catch (SystemException exp)
            {
                throw new Exception(exp.Message);
            }

            return(StrBase64);  
        }