1

鉴于我有以下小部件(以及附加的视图):

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 来帮助绑定。

4

1 回答 1

1

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.

<ListBox.ItemTemplate>
  <DataTemplate>
    <ContentControl Width="100" Height="100" cal:View.Model="{Binding}" />
  </DataTemplate>
</ListBox.ItemTemplate>

Note that you could also use a Conductor such as Conductor<IScreen>.Collection.OneActive or Conductor<IScreen>.Collection.AllActive as your WidgetWorkspaceViewModel type rather than maintain your own Screen list. These conductors have an Items collection, and support screen life cycle.

于 2011-03-08T08:01:45.570 回答