How do I provide development-time support for nested view models in Caliburn Micro?

Using VS2013 and Caliburn.Micro 2.0.2

Given this example:

  • a shell view model having a property of a nested view model, and
  • both shells and nested view models with the Name property:

Project

It seems that at design time, the property of the property of the subview is ignored. Is there any way to support this?

 public class NestedViewModel : PropertyChangedBase { private string _name; public string Name { get { return _name; } set { _name = value; NotifyOfPropertyChange(() => Name); } } public NestedViewModel() { Name = "Nested"; } } 

 <UserControl x:Class="WpfApplication1.Views.NestedView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:cal="http://www.caliburnproject.org" xmlns:viewModels="clr-namespace:WpfApplication1.ViewModels" mc:Ignorable="d" d:DataContext="{d:DesignInstance Type=viewModels:NestedViewModel, IsDesignTimeCreatable=True}" cal:Bind.AtDesignTime="True"> <Grid> <Label x:Name="Name" FontSize="16" Background="LightGreen"/> </Grid> </UserControl> 

The green label shows the correct Name for the nested view model in the designer:

Nested

 public class ShellViewModel : PropertyChangedBase { private string _name; private NestedViewModel _nestedViewModel; public string Name { get { return _name; } set { _name = value; NotifyOfPropertyChange(() => Name); } } public NestedViewModel NestedViewModel { get { return _nestedViewModel; } set { if (Equals(value, _nestedViewModel)) return; _nestedViewModel = value; NotifyOfPropertyChange(() => NestedViewModel); } } public ShellViewModel() { NestedViewModel = new NestedViewModel(); Name = "Shell"; } } 

 <UserControl x:Class="WpfApplication1.Views.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:viewModels="clr-namespace:WpfApplication1.ViewModels" xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d" d:DataContext="{d:DesignInstance Type=viewModels:ShellViewModel, IsDesignTimeCreatable=True}" cal:Bind.AtDesignTime="True"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <ContentControl Grid.Row="0" x:Name="NestedViewModel"/> <Label Grid.Row="1" x:Name="Name" FontSize="16" Background="RoyalBlue"/> </Grid> </UserControl> 

The green label should show the constructor property of the nested Name view in the constructor, but instead display the value of the shell view model:

Shell

It communicates correctly at runtime:

Run time

Example

+6
source share

Source: https://habr.com/ru/post/985670/


All Articles