DataContext not set for child views at design time. #379

Closed
kayle opened this Issue Oct 14, 2016 · 7 comments

Projects

None yet

4 participants

@kayle
kayle commented Oct 14, 2016 edited

I'm starting to investigate various design-time problems. I found 4d2aaf7 prevents the DataContext from being set.

Sample Wpf project setup:
ShellView.xaml (with )

     xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform"
     cal:Bind.AtDesignTime="True"
     d:DataContext="{d:DesignInstance Type=local:ShellViewModel,IsDesignTimeCreatable=True}"

  <StackPanel Background="White">
    <TextBlock x:Name="Text"></TextBlock>
    <ContentControl x:Name="Child"></ContentControl>
  </StackPanel>

ChildView.xaml

<TextBlock x:Name="Text"></TextBlock>

and the following simple view models:

  public class ShellViewModel : PropertyChangedBase
  {
    public string Text { get; set; } = "Shell Text";
    public ChildViewModel Child { get; set; } = new ChildViewModel();
  }

  public class ChildViewModel : PropertyChangedBase
  {
    public string Text { get; set; } = "Child Text";
  }

The designer for ShellView.xaml incorrectly shows
image
whereas it should match the runtime behavior:
image

I think the simplest fix is to change the fix from @brendankowitz to

if (e.NewValue == e.OldValue || (Execute.InDesignMode && e.NewValue is string)) {
    return;
}
@eogas
eogas commented Oct 14, 2016

Potentially related issues #214, #358, and #359

@eogas
eogas commented Oct 14, 2016 edited

No idea if this is the right fix or not, but FWIW, the proposed solution fixes the issue in my reproducer case here: https://github.com/eogas/CaliburnMicroDesignTimeNestedBindingBug

@superware
superware commented Oct 27, 2016 edited

I also accidentally stumbled upon this faulty behavior. Very simple to reproduce, and frankly quite annoying.

Is there any project that doesn't have nested views and doesn't need to visualize them cascading in the designer? a strange one.

@eogas which proposed solution?

@eogas
eogas commented Oct 27, 2016 edited

@superware The change at the end of @kayle's report is what I'm referring to.

if (e.NewValue == e.OldValue || (Execute.InDesignMode && e.NewValue is string)) {
    return;
}

I don't know enough about the project to say whether or not that is a "correct" solution, but it does fix the specific issue mentioned in the bug.

EDIT: Maybe we can prod @nigel-sampson to take a quick look and advise whether on or not this seems like a legit fix?

@nigel-sampson
Contributor

Sorry guys, have been living out of a suit case without internet for the last few weeks.

Will take a closer look into this.

@superware

Hi Nigel, have you managed to reproduce this issue?

@nigel-sampson
Contributor

Just pushed that change (thanks @brendankowitz). Hoping to have 3.0.2 out in a few days. Apologies for the delays.

@nigel-sampson nigel-sampson added the bug label Nov 9, 2016
@nigel-sampson nigel-sampson added this to the v3.0.2 milestone Nov 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment