简体   繁体   中英

Databinding CustomControls within PivotControls

I'm trying to use a PivotControlPage to control the paging of an object with a List

My current attempt is like so

    <controls:Pivot x:Name="quizPivot" Title="MY APPLICATION" ItemsSource="{Binding Questions}" SelectedIndex="1" >

        <controls:PivotItem Header="{Binding QuestionTitle }">
            <Grid>
                <local:Text5Control DataContext="{Binding .}"></local:Text5Control>
            </Grid>
        </controls:PivotItem>

    </controls:Pivot>

I want the pivot control to control the "next" and "previous" actions and pass that current item to the DataContext of my custom control.

I'm doing this slightly wrong I think, but i'm setting the DataContext of the pivotcontrol and the currentitem in the code behind.

this is where i set the datacontext for the pivot control

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        quiz = new Quiz();
        quiz.Questions = loadQuestions()  // loads questions from file

        quizPivot.DataContext = quiz;
    }

I changed the xaml to look like this.

    <controls:Pivot x:Name="quizPivot" Title="Mensa" SelectedIndex="1" DataContext="{Binding quiz}" ItemsSource="{Binding Questions}">
        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding QuestionTitle}" />
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <local:Text5Control DataContext="{Binding .}"></local:Text5Control>
                </Grid>
            </DataTemplate>
        </controls:Pivot.ItemTemplate>
    </controls:Pivot>

What I was missing is the use of templates for the data. PivotItem is only applicable for static items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM