【问题标题】:Caliburn.Micro binding issueCaliburn.Micro 绑定问题
【发布时间】:2016-03-19 22:54:47
【问题描述】:

我有一个包含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(窗口)

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

LeftView.xaml(用户控件)

<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 (用户控件)

<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>

【问题讨论】:

    标签: c# wpf caliburn.micro


    【解决方案1】:

    好的。看来我不应该在 ShellView 和 LeftView 中都使用cal:Bind.Model。在 LeftView 中,我将 cal:Bind.Model 更改为 cal:View:Model,这样就成功了。

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

    绑定模型

    允许绑定现有视图。在根用户控件、页面和窗口上使用;不在 DataTemplate 中。

    视图.模型

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      相关资源
      最近更新 更多