6

使用 VS2013 和 Caliburn.Micro 2.0.2

鉴于以下示例:

  1. 具有嵌套视图模型属性的外壳视图模型,以及
  2. 外壳和嵌套视图模型都有一个Name属性:

项目

似乎在设计时嵌套视图模型属性被忽略了。有没有办法支持这一点?

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>

绿色标签显示了Name设计器中嵌套视图模型的正确性:

嵌套

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>

绿色标签应该Name在设计器中显示嵌套视图模型属性,而是显示外壳视图模型的值:

壳

它确实在运行时正确绑定:

运行

例子

4

0 回答 0