Windows Universal App Pivot绑定和一个静态PivotItem

时间:2017-04-26 22:29:56

标签: data-binding pivot win-universal-app pivotitem

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

这就是我尝试过的。如果我删除了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>

1 个答案:

答案 0 :(得分:0)

  

甚至可以混合使用吗?

据我所知,这是不可能的。但您可以使用其他方式来满足您的要求。你真正想要做的是第一个PivotItem与其他人有不同的内容。您应该可以使用Data​Template​Selector来为第一项(标题为设置)选择不同的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; }
}