【问题标题】:Render ItemsControl item ContentPresenters on top of each other将 ItemsControl 项 ContentPresenters 相互叠加
【发布时间】:2019-04-10 00:12:28
【问题描述】:

我有一个包含可观察集合属性的视图模型:

public ObservableCollection<ExplorerPane> Panes { get; set; } = new ObservableCollection<ExplorerPane>();

在与我的视图模型对应的用户控件中,我使用Panes 作为ItemsSourceItemsControl,并且只使用ContentPresenter 来显示每个ExplorerPane 的内容:

<ItemsControl ItemsSource="{Binding Panes}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

问题是我希望内容演示者将他们的窗格在彼此的顶部呈现,这样一次只有一个可见。

我已经想到了一个解决方案:"Controls in the same cell of a Grid are rendered back-to-front. So a simple way to put one control on top of another is to put it in the same cell."

然后我的问题是如何让内容演示者位于网格的同一个单元格中?

【问题讨论】:

  • 如果您只想显示单个 ExplorerPane,请将 CurrentPane 属性添加到您的视图模型中,并通过 &lt;ContentControl Content={Binding CurrentPane}"/&gt; 显示它
  • 这个建议有效;如果您更新答案,我会将其标记为已接受。

标签: c# wpf xaml


【解决方案1】:

使用网格作为 ItemsPanel:

<ItemsControl ItemsSource="{Binding Panes}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

ItemTemplate 不是必需的,因为 ItemsControl 已经使用 ContentPresenter 作为项目容器。


但是,如果您只想显示单个 ExplorerPane,请将 CurrentPane 属性添加到您的视图模型,并显示它

<ContentControl Content={Binding CurrentPane}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多