【问题标题】:UWP: ScrollViewer inside an ItemsPanelUWP:ItemsPanel 中的 ScrollViewer
【发布时间】:2016-06-01 08:28:06
【问题描述】:

我想创建一个数据透视表,将数据绑定到一个数据透视表项列表。 这些项目中的每一个都应该被一个 ScrollViewer 包围。 不幸的是,它并没有按照我的想法工作......

我的代码:

主页:

<Page
x:Class="PivotSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PivotSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Pivot Title="Pivot" SelectedItem="{Binding CurrentPivotItem, Mode=TwoWay}" ItemsSource="{Binding PivotItems}">
        <Pivot.ItemTemplate>
            <DataTemplate>
                <ScrollViewer>
                    <UserControl Content="{Binding Content}"/>
                </ScrollViewer>
            </DataTemplate>
        </Pivot.ItemTemplate>
    </Pivot>
</Grid>

UserControl(第二个UserControl也一样):

<UserControl
x:Class="PivotSample.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PivotSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<StackPanel Background="Yellow">
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
    <TextBlock Text="Test" FontSize="30" HorizontalAlignment="Center"/>
</StackPanel>

视图模型:

public class ViewModelMainPage : ViewModelBase
{
    private IList<PivotItem> _pivotItems;
    private PivotItem _currentPivotItem;

    public IList<PivotItem> PivotItems
    {
        get { return _pivotItems; }
        set { _pivotItems = value; }
    }

    public PivotItem CurrentPivotItem
    {
        get { return _currentPivotItem; }
        set
        {
            OnPropertyChanged(nameof(CurrentPivotItem));
            _currentPivotItem = value;

        }
    }
}

当我启动项目时,出现以下消息:“程序 '[2940] name.exe' 已退出,代码为 -1 (0xffffffff)。

但是当我用 Grid 或 StackPanel 替换 ScrollViewer 时,它会起作用,但我不会看到我的所有 PivotItem。

有什么想法吗?

【问题讨论】:

    标签: c# xaml uwp windows-10-universal uwp-xaml


    【解决方案1】:

    (提出一个新的答案,因为问题已经改变了它的上下文。)

    如果您使用PivotItems 的列表,看起来枢轴控制出了问题' sItemsSource.

    无论如何:这不是 DataBindings 的本意。

    顾名思义,您绑定到 Data,而不是 控件UI 元素PivotItems 是 UI 控件(尽管它们更像是一种逻辑而非视觉表示),不应成为您的 ViewModel 的一部分。

    只需将 ViewModel 中的类型更改为一些实际数据(字符串、对象或您创建的任何类的列表),它就可以正常工作

    或者,如果您想在 Pivot Control 中放置固定内容,则根本不需要绑定它,只需将 PivotItems 直接放在 Xaml 中即可:

    <Pivot>
      <PivotItem Header="First Item">
        <!-- your content display here -->
      </PivotItem>
      <PivotItem Header="Secont Item">
        <!-- your content display here -->          
      </PivotItem>
    </Pivot>
    

    【讨论】:

    • 好的,但我不知道我该怎么做...我想创建一个带有 Pivot 的页面,每个 PivotItem 代表一个 UserControl,在每个 UserControl 中我将有不同的内容。例如,一个 UserControl 显示图片,另一个显示一些信息。你知道我该如何实现它吗?也许有一个 ItemControl?如果是怎么办?
    • 在这种情况下,只需将项目放在 Xaml 中(请参阅更新的答案)。
    【解决方案2】:

    (已过时,因为问题已更新)

    您只能将ItemControls 设置为 ItemsPanelTemplate,并且 Scrollviewer 不是 ItemsControl,而是 ContentControl(它只包装一个子项,不显示多个项)。

    此外,ItemsPanelTemplate 是您尝试实现的错误目标:如果您想更改 Pivot 内 PivotItems 的内容,只需设置常规 ItemTemplate

    <Pivot>
      <Pivot.ItemTemplate>
        <DataTemplate>
          <ScrollViewer>
            <!-- your content display here -->
          </ScrollViewer>
        </DataTemplate>
      </Pivot.ItemTemplate>
    </Pivot>
    

    【讨论】:

    • 刚刚试了一下,效果很好。也许你的DefaultScrollViewer 风格会破坏一些东西。
    • 我想我做错了什么......code code
    • 是的。那里已经有一个 PivotItem,你不需要把它放在模板中。在那里,您只需定义 PivotItem 中放置的内容。
    • 然后把你的 UserControl 放在我当前评论说“你的内容在这里”的地方。
    • 然后在 Visual Studio 中,查看实时可视化树并查看实际创建的内容。 Pivots 是由 6 个网格、一个 ScrollViewer、一个 PivotItemsPresenter、多个隐藏按钮等组成的。如果此时无法滚动,则很可能是您的用户控件没有要求足够的高度。
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多