我有一个包含ShellView的LeftView,它本身包含一个LeftTopView。我遇到的问题是,LeftTopView没有显示来自LeftTopViewModel的信息。
当我将引导程序更改为使用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 (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>发布于 2015-12-14 15:50:40
好的。我似乎不应该同时使用来自ShellView和LeftView的LeftView。在LeftView中,我将cal:Bind.Model更改为cal:View:Model,这就实现了这一工作。
根据这些依赖项属性的工具提示。
Bind.Model
允许在现有视图上绑定。在根UserControls、页面和窗口上使用;不在DataTemplate中使用。
View.Model
将模型附加到UI的依赖项属性。
https://stackoverflow.com/questions/34267388
复制相似问题