我想要创建一个支点,其中包含一个DataBinding和一个PivotItems列表。这些项目中的每一个都应该被一个ScrollViewer包围。不幸的是,这不像我想的那样.
我的代码:
MainPage:
<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>
ViewModel:
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 (0 0xffffffff)。
但是,当我用网格或ScrollViewer替换StackPanel时,它会正常工作,但这样我就看不到所有的PivotItem了。
有什么想法吗?
发布于 2016-06-02 15:20:49
(提出一个新的答案,因为这个问题已经改变了它的上下文。)
如果您在PivotItems的ItemsSource上使用了一个列表,那么数据透视控件看起来就像搞砸了。
无论如何,这不是DataBindings的本意。
顾名思义,您绑定到数据,而不是绑定到控件或UI元素。PivotItems是UI控件(尽管它们更像是一个逻辑而不是可视化的表示),不应该是您的ViewModel的一部分。
只需将ViewModel中的类型更改为一些实际数据(创建的字符串、对象或其他类的列表),它就能正常工作。
或者,如果您想在数据透视控件中放置固定内容,则根本不需要绑定它,您可以直接将PivotItems放在Xaml中:
<Pivot>
<PivotItem Header="First Item">
<!-- your content display here -->
</PivotItem>
<PivotItem Header="Secont Item">
<!-- your content display here -->
</PivotItem>
</Pivot>发布于 2016-06-01 09:58:14
(问题已更新,已过时)
您只能将ItemControls设置为ItemsPanelTemplate,而Scrollviewer不是ItemsControl,而是ContentControl (它只包装一个子项目,不显示多个项)。
另外,ItemsPanelTemplate是您试图实现的错误目标:如果您想在数据透视中更改PivotItems的内容,只需设置常规的ItemTemplate即可。
<Pivot>
<Pivot.ItemTemplate>
<DataTemplate>
<ScrollViewer>
<!-- your content display here -->
</ScrollViewer>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>https://stackoverflow.com/questions/37563307
复制相似问题