将 ListBox 绑定到屏幕集合(小部件)
鉴于我有以下小部件(以及附加的视图):
public class MyWidgetViewModel : Screen {}
我可以在另一个视图中轻松使用和显示它。 “托管”小部件的视图模型如下所示:
public class WidgetWorkspaceViewModel : Screen
{
private Screen myWidget = new MyWidgetViewModel();
public Screen MyWidget
{
get { return myWidget; }
set { myWidget = value; }
}
}
并且控件在 WidgetWorkspaceView 上显示如下:
<ContentControl x:Name="OneWidget"/>
这一切都运行良好。但现在我想更改它,以便 WidgetWorkspaceViewModel 将包含一组 Widget,并且 WidgetWorkspaceView 应该一个接一个地显示它们。我在让它发挥作用时遇到了一些问题。
视图模型应该如下所示:
private List<Screen> widgets;
public List<Screen> Widgets
{
get { return widgets; }
set { widgets = value; NotifyOfPropertyChange(() => Widgets); }
}
这部分代码没问题,因为我已将小部件绑定到组合框,并且组合框正确显示了控件列表。但我在视图上“托管”小部件时遇到问题。这是我尝试在 WidgetWorkspaceView 中使用的 XAML,但没有成功:
<ListBox x:Name="Widgets">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Width="100" Height="100" DataContext="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
基于断点,数据绑定部分工作正常,但我没有绘制小部件。
有什么想法如何解决这个问题吗?我正在使用 Caliburn.Micro 来帮助进行绑定。
Given that I have the following widget (and a view attached to it):
public class MyWidgetViewModel : Screen {}
I can use and display it easily in an another view. The view model "hosting" the widget looks like the following:
public class WidgetWorkspaceViewModel : Screen
{
private Screen myWidget = new MyWidgetViewModel();
public Screen MyWidget
{
get { return myWidget; }
set { myWidget = value; }
}
}
And the control is shown on WidgetWorkspaceView like this:
<ContentControl x:Name="OneWidget"/>
This all works nicely. But now I would like to change it so that the WidgetWorkspaceViewModel would contain a collection of Widgets and the WidgetWorkspaceView should show them one after another. And I'm having some problems getting this to work.
The view model is supposed to look like this:
private List<Screen> widgets;
public List<Screen> Widgets
{
get { return widgets; }
set { widgets = value; NotifyOfPropertyChange(() => Widgets); }
}
This part of the code is OK, because I've binded the Widgets to a combobox and the combobox shows a list of controls correctly. But I'm having problems "hosting" the widgets on the view. This is the XAML I've tried to use in the WidgetWorkspaceView but without a success:
<ListBox x:Name="Widgets">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Width="100" Height="100" DataContext="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Based on the breakpoints, the databinding part is working OK but I'm not getting the widgets drawn.
Any ideas how to solve this? I'm using Caliburn.Micro to help out with the bindings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题是 Caliburn.Micro 会自动为 ItemsControl 定义 ItemTemplate,您将覆盖它,因此您需要从 ListBox 中删除它,或者如果您想定义它,则使用 < code>View 附加属性来设置模型。
请注意,您还可以使用
Conductor
,例如Conductor.Collection.OneActive
或Conductor.Collection.AllActive
作为您的WidgetWorkspaceViewModel
类型,而不是维护您自己的Screen
列表。这些导体有一个Items
集合,并支持屏幕生命周期。I think the problem is that Caliburn.Micro will automatically define the ItemTemplate for an ItemsControl, which you'll be overriding, so you'll either need to remove that from your ListBox, or if you want to define it, then use the
View
attached property to set the model.Note that you could also use a
Conductor
such asConductor<IScreen>.Collection.OneActive
orConductor<IScreen>.Collection.AllActive
as yourWidgetWorkspaceViewModel
type rather than maintain your ownScreen
list. These conductors have anItems
collection, and support screen life cycle.