CMarinacci
Topic Author
Posts: 11
Joined: 07 Aug 2017, 02:56

Possible issue with bound DataContext (v2.1.0f1 Unity)

13 Mar 2018, 05:55

I have a ToolTip created from a UserControl. The ToolTip's DataContext is bound to a property of its parent's DataContext. I am not changing any of these values in codebehind -- they do not change once they are initially set. The value of the ToolTip's DataContext switches back and forth between the parent's property value and null (which seems kind of strange, since I'm not changing its value). The switch to null occurs after the ToolTip closes, when another ToolTip (using a different UserControl class and a DataContext bound to a different property of its parent's DataContext) on the same screen opens. The switch to the parent's property value occurs just before the ToolTip opens. Sometimes, however, the switch doesn't happen and the DataContext gets stuck on the property value (which is okay), but can also get stuck on null (in which case no data shows up in the ToolTip). I can probably work around this by saving the initial DataContext value and then restoring it if it gets stuck on null, but it doesn't seem like it should be behaving this way.

The following is a very simplified version of how the XAML is structured.

Info is a class with information.
InfoView is a UserControl type that visually shows data of the Info type.
CurrentInfo is an instance of the Info type that is a property of Parent's DataContext.
<UserControl x:Name="Parent">
    <Grid>
        <Grid.ToolTip>
            <local:InfoView DataContext="{Binding CurrentInfo}" />
        </Grid.ToolTip>
    </Grid>
</UserControl>
I attached an event handler to DataContextChanged to see what is going on. The event is firing correctly to show when the DataContext is changed. This is the stack trace when DataContext changes:

Void Noesis.FrameworkElement:RaiseDataContextChanged (IntPtr, IntPtr, IntPtr)+0x7d at NoesisGUI\Plugins\API\Proxies\FrameworkElement.cs:190 C#
Void Noesis.View:Noesis_View_Update_ (HandleRef, Double)+0x3 at NoesisGUI\Plugins\API\Core\NoesisView.cs:528 C#
Void Noesis.View:Update (Double)+0x17 at NoesisGUI\Plugins\API\Core\NoesisView.cs:294 C#
Void NoesisView:LateUpdate ()+0x45 at NoesisGUI\Plugins\NoesisView.cs:674 C#

It appears that I can stop the switching from happening after the ToolTip closes if a plain ToolTip (just text) is shown before another non-text ToolTip is shown.

Is there something that I could possibly be doing that causes the DataContext to behave like this, or is this a bug?

Thanks,

Chris
 
User avatar
sfernandez
Site Admin
Posts: 1309
Joined: 22 Dec 2011, 19:20

Re: Possible issue with bound DataContext (v2.1.0f1 Unity)

13 Mar 2018, 14:26

Hi,

It doesn't look like a bug the way you set up the xaml. As you are setting InfoView.DataContext property using a binding that depends on its logical's parent DataContext, and ToolTip is connected and disconnected from the UI tree everytime is shown, that is why you are getting DataContext changes. If you want to avoid that behavior you can bind using a named element instead:
<UserControl x:Name="Parent">
    <Grid>
        <Grid.ToolTip>
            <local:InfoView DataContext="{Binding DataContext.CurrentInfo, ElementName=Parent}" />
        </Grid.ToolTip>
    </Grid>
</UserControl>
It appears that I can stop the switching from happening after the ToolTip closes if a plain ToolTip (just text) is shown before another non-text ToolTip is shown.
Although this is something I don't understand. Are you sure DataContext is not set to null if you do that?
 
CMarinacci
Topic Author
Posts: 11
Joined: 07 Aug 2017, 02:56

Re: Possible issue with bound DataContext (v2.1.0f1 Unity)

13 Mar 2018, 17:30

The DataContext doesn't change to null when the ToolTip closes, it changes to null when another non-text ToolTip connected to a different control opens.

Opening a text-only ToolTip seems to be what stops the DataContext switching behavior. The DataContext does not switch after this, so it stays in whatever state it was in.

Case 1:
Open non-Text ToolTip 1
ToolTip 1 DataContext switches to non-null
Close ToolTip 1
Open non-text ToolTip 2
ToolTip 1 DataContext switches to null
Close ToolTip 2
Open text-only ToolTip 3
Close ToolTip 3
Open ToolTip 1
ToolTip 1 DataContext does not switch and stays null forever

Case 2:
Open non-Text ToolTip 1
ToolTip 1 DataContext switches to non-null
Close ToolTip 1
Open text-only ToolTip 3
Close ToolTip 3
Open non-text ToolTip 2
Close ToolTip 2
Open ToolTip 1
ToolTip 1 DataContext does not switch and stays non-null forever

I'm pretty sure this is the behavior. I have a breakpoint set in ToolTip 1's DataContextChanged event handler to see when DataContext changes.

Thanks,

Chris
 
User avatar
sfernandez
Site Admin
Posts: 1309
Joined: 22 Dec 2011, 19:20

Re: Possible issue with bound DataContext (v2.1.0f1 Unity)

13 Mar 2018, 22:06

I understand now, thanks for the detailed explanation.
This looks like a bug then, could you please report it in our bugtracker? If possible attach a minimal project to reproduce it.
Thanks.

P.S. Did you try the ElementName binding I suggested? Does it solve the problem?
 
CMarinacci
Topic Author
Posts: 11
Joined: 07 Aug 2017, 02:56

Re: Possible issue with bound DataContext (v2.1.0f1 Unity)

13 Mar 2018, 22:57

When using the ElementName binding, the code works correctly. The ToolTip's DataContext is set once when the parent UserControl loads, and never changes after that.

I'll report it in the bugtracker. I'll see if I can come up with a simple project that reproduces it, but it may not be possible to make this happen with a simple project.

Thanks,

Chris
 
User avatar
sfernandez
Site Admin
Posts: 1309
Joined: 22 Dec 2011, 19:20

Re: Possible issue with bound DataContext (v2.1.0f1 Unity)

14 Mar 2018, 11:06

Thanks for your collaboration Chris.

Who is online

Users browsing this forum: No registered users and 7 guests