将 ListBox 绑定到屏幕集合(小部件)

发布于 2024-10-20 22:22:33 字数 1491 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-10-27 22:22:33

我认为问题是 Caliburn.Micro 会自动为 ItemsControl 定义 ItemTemplate,您将覆盖它,因此您需要从 ListBox 中删除它,或者如果您想定义它,则使用 < code>View 附加属性来设置模型。

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

请注意,您还可以使用 Conductor,例如 Conductor.Collection.OneActiveConductor.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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文