question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataContext not set for child views at design time.

See original GitHub issue

I’m starting to investigate various design-time problems. I found https://github.com/Caliburn-Micro/Caliburn.Micro/commit/4d2aaf7e2b9a2334ef792a1f6d9f1790c9a02a7a 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;
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
nigel-sampsoncommented, Oct 31, 2016

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

Will take a closer look into this.

0reactions
nigel-sampsoncommented, Nov 9, 2016

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

DataContext not set when using View as DataTemplate in ...
When I add ViewModels to my collection, an appropriate number of child Views are generated in the ItemsControl.
Read more >
Usable WPF Design-Time Data for .NET 5.0 (and 6.0)
I find design-time support essential for successful WPF view design, however much of it is broken with .NET 5.0. What to do? I...
Read more >
WPF/MVVM: Child control recognize DataContext as Model ...
To fix this issue, you need to set the Command binding of the MenuItem to the MainWindowViewModel instead of the Project object. You...
Read more >
Using Design-time Databinding While Developing a WPF ...
A trick that allows populating a user control with sample data while you are designing it in the Visual Studio designer.
Read more >
WPF – .Net Diaries
While developing WPF applications, design view plays an important role not only placing the controls but also we can see the run time...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found