How to have design time support for nested view models in Caliburn Micro?
Asked Answered
D

0

6

Using VS2013 and Caliburn.Micro 2.0.2

Given this example of:

  1. a shell view model that has a nested view model property, and
  2. both the shell and nested view models having a Name property:

Project

It seems that during design time the nested view model property is ignored. Is there a 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 nested view model Name property in the designer, but instead shows the value of the shell view model:

Shell

It does bind correctly at runtime:

Run time

Example

Disintegrate answered 22/4, 2015 at 10:57 Comment(5)
When name of property is different in ShellViewModel and NestedViewModel you will see only RoyalBlue Label.Period
@Period Yes. It is another indicator that nested view models aren't bound correctly. I felt for the purposes of illustration it was clearer to demonstrate with properties of the same name.Disintegrate
you might want to notify this as an issue on CM githubRubicund
@Rubicund Good idea: github.com/Caliburn-Micro/Caliburn.Micro/issues/214Disintegrate
I'm voting to close this question as off-topic because that's a bug in that library - issue reported to authors, waiting for a fix.Dorinda

© 2022 - 2024 — McMap. All rights reserved.