DataContext not set for child views at design time.
See original GitHub issueI’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 whereas it should match the runtime behavior:
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:
- Created 7 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry guys, have been living out of a suit case without internet for the last few weeks.
Will take a closer look into this.
Just pushed that change (thanks @brendankowitz). Hoping to have
3.0.2
out in a few days. Apologies for the delays.