Windows 通用 App Pivot 绑定和一个静态 PivotItem

Windows Universal App Pivot Binding and one Static PivotItem

我尝试将数据透视表绑定到 Windows 通用应用程序中的 DataContext。 一切正常,除了我似乎无法混合绑定和 "static" PivotItems。 我需要基于列表和包含设置的静态 PivotItem 创建 0 到 n PivotItem。

这是我试过的。如果我删除 HeaderTemplate 和 ItemTemplate 元素,则会显示 PivotItem-Element。如果我让模板元素放在那里,则显示绑定数据但不显示额外的 PivotItem。

还能混用吗?

<Pivot Name="PivotMain" Title="Title" ItemsSource="{Binding Path=Parts}">
            <Pivot.HeaderTemplate>
                <DataTemplate x:DataType="viewmodel:DetailModel">
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </Pivot.HeaderTemplate>
            <Pivot.ItemTemplate>
                <DataTemplate x:DataType="viewmodel:DetailModel">
                    <TextBlock Text="TestTest"/>
                </DataTemplate>
            </Pivot.ItemTemplate>
            <PivotItem Name="Settings" Header="Settings">
                <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto">
                    <ListView ItemsSource="{Binding Path=Settings}">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="viewmodel:SettingModel">
                                <RelativePanel>
                                    <ToggleSwitch Name="OnOff"
                                                  OffContent="{Binding OffContent}" OnContent="{Binding OnContent}" IsOn="{Binding IsMonitored, Mode=TwoWay}"
                                                  RelativePanel.AlignLeftWithPanel="True" />
                                </RelativePanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </ScrollViewer>
            </PivotItem>
        </Pivot>

Is it even possible to mix?

据我所知,这是不可能的。但是您可以使用其他方式来满足您的要求。你真正想要做的是第一个 PivotItem 与其他内容不同。您应该能够使用 Data​Template​Selector,您可以 select 与其他人一起为第一项(header 是设置)使用不同的 DataTemplate。例如代码如下:

XAML

<Page.Resources>
   <DataTemplate x:Key="itemstemplate" x:DataType="local:DetailModel">
       <TextBlock Text="TestTest"/>
   </DataTemplate>
   <DataTemplate x:Key="settingtemplate" x:DataType="local:DetailModel">
       <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto">
           <ListView ItemsSource="{Binding Path=Settingss}">
               <ListView.ItemTemplate>
                   <DataTemplate x:DataType="local:SettingModel">
                       <RelativePanel>
                           <ToggleSwitch Name="OnOff" OffContent="{Binding OffContent}" OnContent="{Binding OnContent}" IsOn="{Binding IsMonitored, Mode=TwoWay}" RelativePanel.AlignLeftWithPanel="True" />
                       </RelativePanel>
                   </DataTemplate>
               </ListView.ItemTemplate>
           </ListView>
       </ScrollViewer>
   </DataTemplate>

   <local:PivotTemplateSelector
       itemstemplate="{StaticResource itemstemplate}"
       settingtemplate="{StaticResource settingtemplate}"
       x:Key="PivotTemplateSelector" />
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <Pivot Name="PivotMain" Title="Title" ItemsSource="{Binding Path=Parts}"  ItemTemplateSelector="{StaticResource PivotTemplateSelector}">       
           <Pivot.HeaderTemplate>
           <DataTemplate x:DataType="local:DetailModel">
               <TextBlock Text="{Binding Name}"/>
           </DataTemplate>
       </Pivot.HeaderTemplate>     
   </Pivot>
</Grid>

代码隐藏

public sealed partial class MainPage : Page
{
    ObservableCollection<DetailModel> Parts;
    ObservableCollection<SettingModel> Settingss;
    public MainPage()
    {
        this.InitializeComponent();
        Settingss = new ObservableCollection<SettingModel>()
        {
            new SettingModel()
            {
                 IsMonitored=true,
                 OffContent="work at",
                 OnContent="content"
            }
        };
        Parts = new ObservableCollection<DetailModel>()
        {
            new DetailModel()
            {
                Name="Settings",
                Settingss=Settingss
            },
            new DetailModel()
            {
                Name="test1"
            },
            new DetailModel()
            {
                Name="test2"
            }
        };

        datasources datasource = new datasources()
        {
            Parts = Parts
        };
        this.DataContext = datasource;
    }
}
public class PivotTemplateSelector : DataTemplateSelector
{
    public DataTemplate itemstemplate { get; set; }
    public DataTemplate settingtemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item)
    {
        DetailModel itemget = item as DetailModel;
        if (itemget.Name == "Settings")
        {
            return settingtemplate;
        }
        else
            return itemstemplate;

        return base.SelectTemplateCore(item);
    }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        return SelectTemplateCore(item);
    }
}
public class datasources
{
    public ObservableCollection<DetailModel> Parts { get; set; }
}

public class DetailModel
{
    public string Name { get; set; }

    public ObservableCollection<SettingModel> Settingss { get; set; }
}

public class SettingModel
{
    public string OffContent { get; set; }
    public string OnContent { get; set; }
    public bool IsMonitored { get; set; }
}