AiNoTame

...for love
posts - 9, comments - 40, trackbacks - 0

INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

Leggendo per caso il blog di Gianni Giaccaglini (http://blogs.wpfitalia.it/GianniGiaccaglini/archive/2011/09/08/Binding-ADO-tramite-classe-ad-hoc.aspx) riguardo il binding verso dataset, ho notato un capitolo, “INotifyPropertyChanged server davvero?” e mi sono ricordato di un problema simile (forse lo stesso) che mi è capitato tempo fa.
Iniziamo dal codice xaml e il datacontext:

<StackPanel>
    <TextBox Text="{Binding Path=PrimaProp}" />
    <TextBox Text="{Binding Path=PrimaProp}" />
    <TextBox Text="{Binding Path=SecondaProp}" />
    <TextBox Text="{Binding Path=SecondaProp}" />
    <Button Content="Cambia" Click="Button_Click" />
</StackPanel>
public class MyDataContext
{
    public string PrimaProp { get; set; }
    public string SecondaProp { get; set; }
}

Qualcuno noterà che MyDataContext (nel codebehind viene impostata un istanza come DataContext) non implementa INotifyPropertyChanged.
Modificando una textbox ci si aspetterebbe che le altre non cambino perchè non si notifica alla vista che la proprietà è stata cambiata… E invece tutto funziona!
Ai tempi pensai che l’engine di wpf fosse abbastanza intelligente da usare un oggetto binding uguale se viene scritto uguale in più parti… invece no Smile.
Se proviamo infatti a impostare via codice il binding (es: textbox1.SetBinding(TextBox.TextProperty, new Binding(“PrimaProp”))), con nuove istanze di Binding su diversi oggetti, continua ancora a funzionare.

Proviamo a cambiare la proprietà del datacontext da codice tramite il bottone con:
(this.DataContext as MyDataContext).SecondaProp=”modifica da codice”… NON FUNZIONA!

Non sapendo dove andare a parare, ho cercato con google, e infine ho trovato la soluzione indicata qui: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9365bb6a-b411-4967-9a03-ae2a810fb215/

Sunto della soluzione:
- Se un oggetto CLR implementa INotifyPropertyChanged, è cura del developer lanciare l’evento NotifyPropertyChanged
- Se un oggetto CLR NON implementa INotifyPropertyChanged, il binding lavora sul TypeDescriptor dell’oggetto. Esso va a registrare alla modifica delle proprietà tramite PropertyDescriptor.AddValueChanged().

Quindi:
1) come far andare il codice precedente riguardo il bottone:
”semplice” (?!?) basta recuperare la PropertyDescriptor dell’oggetto e impostare così la proprietà:
var propertyDescriptor = TypeDescriptor.GetProperties(this.DataContext).Find("SecondaProp", false);
propertyDescriptor.SetValue(this.DataContext, "ModificaDaCodice");

2) come far smettere di andare il precedente esempio:
Implementiamo l’interfaccia INotifyPropertyChanged e propaghiamo l’evento solo per PrimaProp. Questa funziona, ma SecondaProp no Smile.

Performance (mentre l’ho fatto ho sentito l’esigenza di un altro post)

Se eseguo il codice nel thread UI, le performance sono molto simili.
Se eseguo il codice in un altro thread, l’utilizzo di INotifyPropertyChanged risulta più rapido rispetto a PropertyDescriptor.

Conclusioni:

Data la flessibilità e la maggior facilità implementativa, la mia preferenza ricade indubbiamente a INotifyPropertyChanged. Le performance inoltre sembrerebbero avvalorare tale scelta. Quindi Me.NofifyPropertyChanged(“you”);

Print | posted on Saturday, September 10, 2011 11:47 PM | Filed Under [ Binding ]

Feedback

Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

When you're in the corner and have no money to go out from that point, you would require to take the mortgage loans. Just because that will help you for sure. I get car loan every year and feel OK because of that.
9/13/2011 6:08 PM | PALMERRosanne
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

Are thirsting for better traffic? Don't realize how to do that? We are here for such a case! We provide premium quality article submissions issues at ezine article submission company. We will guarantee you great publicity and success of your internet commerce.
9/26/2011 1:32 AM | PierceLIZA20
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

People's life seems to be short, hence we must live it fast. Moreover, the do my essay service would help us to construct our academic career very fast.
10/20/2011 12:26 AM | DeckerMOLLIE
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

’utilizzo di INotifyPropertyChanged risulta più rapido rispetto a PropertyDescriptor.
10/21/2011 7:18 AM | Telephone Answering Service
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

That the Boys and Women Clubs of America a superb task, evident through the fact that UGG Australia this organization's assist. Each sold UGG Australia Classic (maybe together with your possess style), component of its income to this organization. The next is integrated around the site with the Boys and Girls Clubs of The us: "Boys & Girls Clubs are a safe place to learn and grow - all uggs clearance whilst having fun. It's the location where excellent futures are started Each and each and every day. "We have nothing far more to add. Should we now all have produced ??good, uggs buy you have inspired and want to determine how you must act now? For this action by UGG Australia a special web site was set up, where everything is explained in excellent detail. Also can you download a template you can design your creation. Oh yes, if you join, please allow us know. Then we promote your style on Timberland Fanzone!
12/20/2011 9:56 AM | juliosharpe
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

I charge we appreciate you your time and efforts you acquire in publishing this blog. Actually your creative thinking ability has encouraged me to start out my own blog.
1/17/2012 7:56 AM | android developers
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

The loans seem to be essential for people, which want to ground their organization. As a fact, that's not hard to get a college loan.
2/1/2012 5:57 AM | Hogan29Alissa
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

Lots of lecturers require to see the writing technique, hence, we need to opt for custom dissertation to order free dissertation. In such way people improve academic level. And as well you should take custom thesis.
2/11/2012 11:58 PM | EricksonSHERYL
Gravatar

# re: INotifyPropertyChanged serve davvero? Sì! Ma le cose si fanno complicate…

This was exactly what i was searching for. Have been fighting for a while to do this, thanks for have posted
2/16/2012 6:36 AM | web hosting

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 7 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET