Caliburn.Micro 绑定问题
Caliburn.Micro binding issue
我有一个包含 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(用户控件)
<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>
好的。看来我不应该同时使用 ShellView 和 LeftView 中的 cal:Bind.Model
。在 LeftView 中,我将 cal:Bind.Model
更改为 cal:View:Model
,这使这项工作有效。
根据这些依赖属性的工具提示。
Bind.Model
允许绑定到现有视图。在根用户控件、页面和 Window 上使用;不在数据模板中。
View.Model
依赖项 属性 用于将模型附加到 UI
我有一个包含 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(用户控件)
<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>
好的。看来我不应该同时使用 ShellView 和 LeftView 中的 cal:Bind.Model
。在 LeftView 中,我将 cal:Bind.Model
更改为 cal:View:Model
,这使这项工作有效。
根据这些依赖属性的工具提示。
Bind.Model
允许绑定到现有视图。在根用户控件、页面和 Window 上使用;不在数据模板中。
View.Model
依赖项 属性 用于将模型附加到 UI