cimbalino / Cimbalino-Phone-Toolkit

Cimbalino Windows Phone Toolkit
MIT License
78 stars 32 forks source link

Multibinding issue #30

Closed XVincentX closed 10 years ago

XVincentX commented 10 years ago

Looks like that property bound with your nice control does not get updated if the model is updated

pedrolamas commented 10 years ago

Not sure I understand the issue here, can you provide a code sample for me to check?

XVincentX commented 10 years ago

Sure, here is an example.

http://pastebin.com/tin9TX02

I'm expecting that if i just try to modify property "Number", the entire Converter should be recalled to reevaulate the binding. It does not happens!

pedrolamas commented 10 years ago

The link just shows "This paste has been removed!"... :\

XVincentX commented 10 years ago

I did not!! However

http://nopaste.info/5af3c9795c.html

pedrolamas commented 10 years ago

From my tests, seems to be working perfectly as expected. Please download the following app that uses your code (with quite a few changes): https://dl.dropboxusercontent.com/u/28356/gc/PhoneApp2.zip

For every click on the "Add" button, the Number property will raise the change notification and you'll see a message on the Visual Studio Debug window from the Convert method from the MultiValueConverter instances.

XVincentX commented 10 years ago

I will. Thanl you in meantime.

-----Mensaje original----- De: "Pedro Lamas" notifications@github.com Enviado: ‎23/‎11/‎2013 23.43 Para: "Cimbalino/Cimbalino-Phone-Toolkit" Cimbalino-Phone-Toolkit@noreply.github.com CC: "Vincenzo" darkfrenesy@yahoo.it Asunto: Re: [Cimbalino-Phone-Toolkit] Multibinding issue (#30)

From my tests, seems to be working perfectly as expected. Please download the following app that uses your code (with quite a few changes): https://dl.dropboxusercontent.com/u/28356/gc/PhoneApp2.zip For every click on the "Add" button, the Number property will raise the change notification and you'll see a message on the Visual Studio Debug window from the Convert method from the MultiValueConverter instances. — Reply to this email directly or view it on GitHub.

XVincentX commented 10 years ago

Hello again! Your example works. After some attempts i discovered that all your behaviours do not work if inserted into a templated component. For example (as my software), a templated pivot control bound to DataContext. So the new code is this one: http://nopaste.info/c919233614.html Here the ValueConverter is not called, but also the autofocus behavior is not executed! Could you help me?

pedrolamas commented 10 years ago

Inside the DataTemplate, on the root element (in your sample, the ScrollViewer), reset the DataContext like so:

<ScrollViewer DataContext="{Binding DataContext, ElementName=pivotNumbers}">

Though I haven't tested this, right now all it seems to me is that the DataContext may not be getting inherited on the template, and this might fix the issue!

pedrolamas commented 10 years ago

By the way, do not apply the AutoFocusBehavior there, instead, apply it to the whole Page

XVincentX commented 10 years ago

Ok for autofocus. The code you posted does not work since DataContext of pivotNumber is IObservableCollection, while the ScrollViewer DataContext should be an element of that collection. Honestly i do not know how to do that!

Anyway, without the DataContext in ScrollViewer all works as it should, except for MultiValue behaviours update (first binding looks like ok).

pedrolamas commented 10 years ago

Try to add a x:Name="MyPage" to the root Page and replace this:

<Behaviors:MultiBindingItem Value="{Binding Number, BindsDirectlyToSource=True, Mode=OneWay}"></Behaviors:MultiBindingItem>

by this:

<Behaviors:MultiBindingItem Value="{Binding DataContext.Number, ElementName=MyPage, Mode=OneWay}"></Behaviors:MultiBindingItem>
XVincentX commented 10 years ago

It's not working for the same reason explained before. DataContext is not a NumberInfo object (on which is valid DataContext.NumberInfo), but an ObservableCollection of that!

System.Windows.Data Error: BindingExpression path error: 'Number' property not found on 'System.Collections.ObjectModel.ObservableCollection1[WindDataLib.NumberInfo]' 'System.Collections.ObjectModel.ObservableCollection1[WindDataLib.NumberInfo]' (HashCode=52851724). BindingExpression: Path='DataContext.Number' DataItem='WindInfo.DataPage' (HashCode=59880657); target element is 'Cimbalino.Phone.Toolkit.Behaviors.MultiBindingItem' (Name='null'); target property is 'Value' (type 'System.Object')..

Da: Pedro Lamas notifications@github.com

A: Cimbalino/Cimbalino-Phone-Toolkit Cimbalino-Phone-Toolkit@noreply.github.com Cc: Vincenzo darkfrenesy@yahoo.it Inviato: Domenica 24 Novembre 2013 21:45 Oggetto: Re: [Cimbalino-Phone-Toolkit] Multibinding issue (#30)

Try to add a x:Name="MyPage" to the root Page and replace this:

/Behaviors:MultiBindingItem by this: /Behaviors:MultiBindingItem — Reply to this email directly or view it on GitHub.
pedrolamas commented 10 years ago

If your page DataContext is an ObservableCollection then how do you expect this to work correctly? MVVM good practice would dictate that you to create a ViewModel and set this as the DataContext; inside that ViewModel you will have all properties and commands (including your ObservableCollection).

Bottom line is that you should have a proper ViewModel and not use:

<controls:Pivot x:Name="pivotNumbers" SelectionChanged="pivotNumbers_SelectionChanged" Title="Wind Info" ItemsSource="{Binding}" >

but something like:

<controls:Pivot x:Name="pivotNumbers" SelectionChanged="pivotNumbers_SelectionChanged" Title="Wind Info" ItemsSource="{Binding Items}" >

In the end, this doesn't seem related to the usage of the MultiBindingBehavior, but instead with the fact you're setting your DataContext to an ObservableCollection instead of a proper ViewModel.

XVincentX commented 10 years ago

Apologize for the issue, this if my first WP App! I'm going to try your suggestion right now!

pedrolamas commented 10 years ago

No worries! :)

Given it's your first WP app, maybe this article I wrote a while ago might help (it's for WP7, but should work with WP8 also):

http://channel9.msdn.com/coding4fun/articles/Currency-Converter-for-Windows-Phone-7

XVincentX commented 10 years ago

Hello, I am sorry to bother you again, but even using your tip the code does not work. As last favour, could you please provide a bound working example? I will get out from there using that one! Vincenzo

XVincentX commented 10 years ago

Will you?

pedrolamas commented 10 years ago

Check this: https://dl.dropboxusercontent.com/u/28356/gc/MvvmSample.zip

It's a minor sample of how to properly use MVVM in a situation like you describe.

XVincentX commented 10 years ago

I saw the example, here is the difference between your and mine. The number property is inside the observable collection in my project. Each item has got its number!

pedrolamas commented 10 years ago

Basically, you just change the ObservableCollection<string> to something like ObservableCollection<MyComplexObject> I've updated the sample, so please download it again and take a look at the changes. Given that this is off-topic and not related to any issue with the Cimbalino Toolkit, I'll ask you to take this outside of this thread. If you have any further questions on this MVVM subject, just email me on pedrolamas (at) gmail (dot) com

XVincentX commented 10 years ago

Now it works. I was missing INotify interface on main view model. My apologies.