Caliburn.Micro绑定问题

时间:2015-12-14 12:43:59

标签: c# wpf caliburn.micro

我有一个包含LeftView的ShellView,它本身包含一个LeftTopView。我遇到的问题是LeftTopView没有显示LeftTopViewModel中的信息。

当我将boostrapper更改为使用DisplayRootViewFor<LeftViewModel>();而不是DisplayRootViewFor<ShellViewModel>();时,我会看到组合框中填充了值。

当然,我在这里遗漏了为什么在显示ShellView时组合框没有显示数据的原因。 Caliburn.Micro只能在层级中绑定单个级别吗?

代码如下:

public class ShellViewModel : Screen
{
    public LeftViewModel Left { get; set; }

    public ShellViewModel()
    {
        this.DisplayName = "The title of the application";
        Left = new LeftViewModel() { LeftTop = new LeftTopViewModel() };
    }
}

public class LeftViewModel : PropertyChangedBase
{
    public string LeftTitle { get; set; }
    public LeftTopViewModel LeftTop { get; set; }

    public LeftViewModel()
    {
        LeftTitle = "this is visible";
        LeftTop = new LeftTopViewModel();
    }
}

public class LeftTopViewModel : PropertyChangedBase
{
    public string Title { get; set; }

    public List<string> Names { get; set; }

    public string SelectedName { get; set; }

    public LeftTopViewModel()
    {
        Title = "Left-top title";
        Names = new List<string>() { "Dan", "Mark" };
        SelectedName = "Dan";
    }
}

上述视图模型的xaml文件是这样定义的。

ShellView.xaml(Window)

<Grid>
    <views:LeftView cal:Bind.Model="{Binding Left}" />
</Grid>

LeftView.xaml(UserControl)

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label x:Name="LeftTitle" Grid.Row="0" />
    <views:LeftTopView cal:Bind.Model="{Binding LeftTop}" Grid.Row="1"/>
</Grid>

LeftTopView.xaml(UserControl)

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Label x:Name="Title" Grid.Row="0" />
    <ComboBox x:Name="Names" Grid.Row="1" />
</Grid>

1 个答案:

答案 0 :(得分:1)

确定。似乎我不应该使用ShellView和LeftView中的cal:Bind.Model。在LeftView中,我将cal:Bind.Model更改为cal:View:Model,这使其成功。

根据这些依赖属性的工具提示。

Bind.Model

允许绑定现有视图。在root UserControls,Pages和Window上使用;不在DataTemplate中。

View.Model

将模型附加到UI的依赖属性